Menu
CoddyTech
コース

C#開発者のためのC++

C#の経験をC++17に活かしましょう。値、制御フロー、関数、コンテナ、ヘッダー、クラス、仮想関数呼び出し、RAII、ムーブによる所有権を学びます。連携する図書館デスクを作りましょう。

C#とC++を並べて比較

このコースのレッスンから、同じプログラムをC#とC++で並べています。

  1. 文字列と出力

    C#の文字列は変更不可能なUTF-16の参照オブジェクトですが、C++のstd::stringは変更可能なバイトコンテナです。C++のstd::stringは変更可能なバイト列を所有し、ストリームへの挿入によって値を整形します。長さはUnicode文字ではなく、バイト数を数えます。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5
    6static void Main() {7Console.WriteLine("oak".Length);8}9}
    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 name="oak";std::cout<<name.size()<<"\n";15}
  2. 文字列の比較

    C#の文字列の==はテキストを比較しますが、参照の同一性には別のAPIがあります。C++の等価性も同様に、オペランドの型によって異なります。C++のstd::stringの等価性は内容を比較します。生ポインターの等価性はアドレスを比較します。ユーザー定義クラスでは、意図的に等価性の演算を定義する必要があります。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5
    6  static void Main() {7    object a=3, b="3"; Console.WriteLine(a.Equals(b));8  }9}
    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}
  3. 固定長配列

    C#の配列は、要素が値型であっても参照型です。C++のstd::array自体は値です。C++のstd::array<T,N>は固定サイズの値型です。そのサイズは型の一部であり、通常のコピーでは要素が複製されます。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5
    6static void Main() {7int[] a={4,5,6};Console.WriteLine(a.Length+" "+a[1]);8}9}
    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}
  4. 値のコピー

    C#の構造体は値としてコピーされる一方、クラスは参照として別名参照されます。C++のクラスは、操作によって禁止されていない限り、通常は値としてコピーされます。C++のvectorとstringをコピーすると、それぞれ独立した領域を所有します。生ポインターをコピーしても、指し示すオブジェクトは共有されたままで、所有権は定義されません。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5
    6  static void Main() {7    var a=new List<int>{1,2}; var b=new List<int>(a); b[0]=9; Console.WriteLine(a[0]);8  }9}
    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::vector<int> a{4};auto b=a;b.push_back(9);std::cout<<a.size()<<" "<<b.size()<<"\n";15}
  5. 関数と戻り値

    C#のメソッドは通常、型の中にあります。C++のフリー関数には、それを囲むクラスは必要ありません。C++の関数宣言では、引数と戻り値の型を指定します。戻り値は自動的には表示されません。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5  static int Twice(int n) { return n*2; }6  static void Main() {7    Console.WriteLine(Twice(4));8  }9}
    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}
  6. ストリームと厳密な解析

    C#のTryParseは、変換に成功したかどうかを明示的に報告します。C++のストリームは、抽出後に呼び出し側が確認しなければならない状態を公開します。C++のストリーム抽出は、ストリームの状態を通じて成功を報告します。変換を検証し、契約で入力の完全な一致が求められる場合は、予期しない末尾のトークンを拒否してください。

    C#
    1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5
    6static void Main() {7int n;bool ok=int.TryParse("7",out n);Console.WriteLine(ok+" "+n);8}9}
    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";15}

シラバス

10 章48 レッスン1 プロジェクト48 チャレンジ99 クイズ問題

C++のファイルと値

6 レッスン

条件分岐と反復

5 レッスン

標準コンテナ

5 レッスン

コピーとアルゴリズム

5 レッスン

解析とライフタイム

5 レッスン

C#開発者向けのほかのコース

ほかの言語からC++を学ぶ

よくある質問

C#開発者のためのC++はどんな人向けですか?

すでにC#を書けて、C++を学びたい人向けです。各レッスンは知っているC#のコードから始まり、C++で同じことをどう書くかを示すので、初心者向けの基礎は飛ばせます。

C++の知識は必要ですか?

いいえ。コースは最初のC++プログラムから始まります。必要なのはC#の基礎(変数、条件分岐、ループ、関数、コレクション)です。

C#開発者のためのC++にはレッスンがいくつありますか?

C#開発者のためのC++には48のインタラクティブなレッスンがあり、自分のペースで進められます。

C#開発者のためのC++では何を学べますか?

コースは次の章で構成されています: C++のファイルと値, 条件分岐と反復, 標準コンテナ, コピーとアルゴリズム, and more。

C#開発者のためのC++は無料ですか?

はい。Coddyのすべてのコースは無料で開始できます。レッスンはブラウザで進められ、ダウンロードも登録も不要です。

Coddyのプログラミング言語のイラスト

C#開発者のためのC++を無料で始める

始める