Ruby開発者のためのC++
Rubyの経験をC++17に活かしましょう。値、制御フロー、関数、コンテナ、ヘッダー、クラス、仮想ディスパッチ、RAII、ムーブによる所有権を学びます。連携するライブラリデスクを構築しましょう。
RubyとC++を並べて比較
このコースのレッスンから、同じプログラムをRubyとC++で並べています。
文字列と出力
Rubyの文字列はエンコーディングを持つ変更可能なオブジェクトなので、lengthは文字数を数え、bytesizeはバイト数を数えます。C++のstd::stringは変更可能なバイト列を所有し、ストリームへの挿入では値が書式化されます。長さが数えるのはUnicode文字ではなくバイト数です。
Ruby1word = "café"2puts word.length3puts word.bytesizeC++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string name="oak";std::cout<<name.size()<<"\n";15}テキストの等価性
Rubyの==は値の等価性を調べる一方、equal?はオブジェクトの同一性を調べます。C++の演算子の動作は、宣言されたオペランドの型によって異なります。C++のstd::stringの等価性は内容を比較します。生ポインターの等価性はアドレスを比較します。ユーザー定義クラスには、意図的に定義した等価性演算が必要です。
Ruby1a = ["red"]2b = ["red"]3puts a == b4puts a.equal?(b)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string a="red",b="red";std::cout<<std::boolalpha<<(a==b)<<"\n";15}固定長配列
Rubyの配列は<<で拡張され、オブジェクトへの参照を保持します。一方、C++のstd::arrayはコンパイル時に長さが決まり、値をコピーします。C++のstd::array<T,N>は固定サイズの値型です。サイズは型の一部であり、通常のコピーでは要素が複製されます。
Ruby1items = [4, 8]2items << 123puts items[-1]4p items[0, 2]C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::array<int,3> a{4,5,6};std::cout<<a.size()<<" "<<a.at(1)<<"\n";15}値のコピー
Rubyの代入では参照がコピーされるため、2つの変数が同じArrayまたはStringを指すことがあります。また、dupとcloneで作られるのは浅いコピーだけです。新しい外側のArrayも、同じ内側のオブジェクトを参照します。C++のvectorのコピーでは、要素はそれぞれの型に応じてコピーされるため、vectorのvectorをコピーすると内側のvectorもコピーされます。C++のvectorとstringのコピーは、それぞれ独立した領域を所有します。生ポインタをコピーしても、ポインタが指すオブジェクトは共有…
Ruby1original = [[1]]2copy = original.dup3copy[0] << 24copy << [9]5p original6p copy.sizeC++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::vector<std::vector<int>> a{{1}};auto b=a;b[0].push_back(2);b.push_back({9});15std::cout<<a[0].size()<<" "<<a.size()<<" "<<b[0].size()<<" "<<b.size()<<"\n";16}関数と戻り値
Rubyのメソッドでは、引数や戻り値の型を宣言しません。また、returnキーワードがなくても、メソッドは最後の式の値を返すため、twiceは*に応答する任意のオブジェクトで動作します。引数の数や型が間違っていることがわかるのは、呼び出しが実行されたときだけです。C++の関数シグネチャは静的にチェックされます。C++の関数宣言では引数と戻り値の型を定め、void以外の関数は明示的なreturn文で値を返します。戻り値が自動的に表示されることはありません。
Ruby1def twice(value)2 value * 23end4puts twice(4)5puts twice("ab")C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12int triple(int n){return 3*n;}13int main(){14std::cout<<triple(4)<<"\n";15}ストリームと厳密な解析
Rubyには2つの変換方法があります。Integer(text)は文字列全体が数値でない場合にArgumentErrorを発生させます(ただし、exception: falseを指定するとnilを返します)。一方、String#to_iは先頭の数値部分を読み取り、数値がない場合はエラーを出さずに0を返します。C++のストリームでは、抽出の失敗はストリームの状態で表されます。また、to_iと同様に、>>は数値に含められない最初の文字で読み取りを停止します。C++のストリーム抽出は、ストリームの状態を通じて成功を報告し…
Ruby1word, number = "oak 7".split2puts "#{word}/#{Integer(number)}"3p "12px".to_i4p Integer("12px", exception: false)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::istringstream in("oak 7");std::string word;int n;if(in>>word>>n)std::cout<<word<<"/"<<n<<"\n";15std::istringstream bad("12px");int m=0;std::string rest;if(bad>>m&&!(bad>>rest))std::cout<<"exact\n";else std::cout<<"rejected "<<m<<"\n";16}
シラバス
10 章48 レッスン1 プロジェクト48 チャレンジ99 クイズ問題条件分岐と反復
5 レッスンコピーとアルゴリズム
5 レッスン関数とテンプレート
5 レッスン解析とライフタイム
5 レッスン- 05復習:行の検証
オブジェクトと不変条件
6 レッスン合成と所有権
5 レッスンプロジェクト:図書館カウンター
4 レッスン最終課題
2 レッスンよくある質問
Ruby開発者のためのC++はどんな人向けですか?
すでにRubyを書けて、C++を学びたい人向けです。各レッスンは知っているRubyのコードから始まり、C++で同じことをどう書くかを示すので、初心者向けの基礎は飛ばせます。
C++の知識は必要ですか?
いいえ。コースは最初のC++プログラムから始まります。必要なのはRubyの基礎(変数、条件分岐、ループ、関数、コレクション)です。
Ruby開発者のためのC++にはレッスンがいくつありますか?
Ruby開発者のためのC++には48のインタラクティブなレッスンがあり、自分のペースで進められます。
Ruby開発者のためのC++では何を学べますか?
コースは次の章で構成されています: C++のファイルと値, 条件分岐と反復, 標準コンテナ, コピーとアルゴリズム, and more。
Ruby開発者のためのC++は無料ですか?
はい。Coddyのすべてのコースは無料で開始できます。レッスンはブラウザで進められ、ダウンロードも登録も不要です。