Menu
CoddyTech
コース

Python開発者のためのC++

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

PythonとC++を並べて比較

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

  1. 文字列と出力

    Pythonのstrは変更不可なUnicodeテキストです。C++のstd::stringは、所有権が明示された変更可能なバイト列です。C++のstd::stringは変更可能なバイト列を所有し、ストリームへの挿入によって値を整形します。長さはUnicode文字数ではなく、バイト数を数えます。

    Python
    1print(len("oak"))
    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. 文字列の等価性

    Python の == は値の等価性を調べ、is は同一性を調べます。C++ の演算子の動作は、宣言されたオペランドの型によって異なります。C++ の std::string の等価性は内容を比較します。生ポインターの等価性はアドレスを比較します。ユーザー定義クラスでは、意図的に等価性の演算を定義する必要があります。

    Python
    1a = ["red"]2b = ["red"]3print(a == b)4print(a is 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}
  3. 固定長配列

    Pythonのリストはサイズを変更できる参照コンテナーです。一方、C++のstd::arrayはコンパイル時に長さが決まり、値のコピー動作をします。C++のstd::array<T,N>は固定サイズの値型です。サイズは型の一部であり、通常のコピーでは要素が複製されます。

    Python
    1items = [4, 8]2items.append(12)3print(items[-1])4print(items[: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}
  4. 値のコピー

    Pythonの代入ではオブジェクトが共有され、浅いコピーではネストされたエイリアスが保持されます。C++のvectorのコピーでは、要素がそれぞれの型に応じてコピーされます。C++のvectorとstringのコピーでは、独立したストレージが所有されます。生ポインターをコピーしても、指し示すオブジェクトは共有されたままで、所有権は定義されません。

    Python
    1original = [[1]]2copy = original.copy()3copy[0].append(2)4print(original)5print(sorted({3, 1, 3}))
    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. 関数と戻り値

    Pythonの関数は、動的に呼び出される実行時オブジェクトです。C++の関数シグネチャは静的にチェックされます。C++の関数宣言では、引数と戻り値の型を指定します。戻り値が自動的に出力されるわけではありません。

    Python
    1def twice(value):2    return value * 23print(twice(4))
    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. ストリームと厳密なパース

    Pythonでは、無効な数値テキストをintに変換するとValueErrorが発生します。C++では、ストリームの状態によって抽出の失敗が表されます。C++のストリーム抽出では、ストリームの状態によって成功が報告されます。変換を検証し、入力が正確であることを契約で求められている場合は、予期しない末尾のトークンを拒否してください。

    Python
    1word, number = "oak 7".split()2print(f"{word}/{int(number)}")
    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 レッスン

Python開発者向けのほかのコース

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

よくある質問

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

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

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

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

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

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

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

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

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

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

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

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

始める