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 道测验题

更多面向 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++

开始