Menu
CoddyTech
课程

面向 Ruby 开发者的 C++

将 Ruby 开发经验迁移到 C++17:学习值、流程、函数、容器、头文件、类、虚分派、RAII 和移动所有权。动手构建一个完整的图书馆借阅台。

Ruby 与 C++ 对照

同一个程序分别用 Ruby 和 C++ 编写,取自本课程的课时。

  1. 字符串与输出

    Ruby 的 String 是一种可变对象,带有编码,因此 length 统计字符数,而 bytesize 统计字节数。C++ 的 std::string 持有可变字节序列;流插入会格式化值。长度统计的是字节数,而不是 Unicode 字符数。

    Ruby
    1word = "café"2puts word.length3puts word.bytesize
    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. 文本相等性

    Ruby 中,== 用于比较值是否相等,而 equal? 用于比较对象标识。C++ 运算符的行为取决于声明的操作数类型。C++ 中,std::string 的相等比较会比较内容。原始指针的相等比较会比较地址;用户定义的类需要有意实现相等操作。

    Ruby
    1a = ["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}
  3. 固定大小的数组

    Ruby 数组会通过 << 增长,并保存对其对象的引用;C++ std::array 的长度在编译时确定,并按值复制。C++ std::array<T,N> 是一种固定大小的值类型。它的大小是其类型的一部分,普通复制会复制其中的元素。

    Ruby
    1items = [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}
  4. 值的副本

    Ruby 的赋值会复制引用,因此两个变量可以指向同一个数组或字符串。dup 和 clone 只会创建浅拷贝:新的外层数组仍然指向相同的内部对象。C++ 的 vector 拷贝会根据元素的类型复制元素,因此拷贝由 vector 组成的 vector 时,内部 vector 也会被拷贝。C++ 的 vector 和 string 拷贝拥有独立的存储空间。拷贝原始指针仍会共享其指向的对象,而且不会定义所有权。

    Ruby
    1original = [[1]]2copy = original.dup3copy[0] << 24copy << [9]5p original6p copy.size
    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<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}
  5. 函数与返回值

    Ruby 方法不声明参数类型或返回类型。即使没有 return 关键字,方法也会返回最后一个表达式的值,因此 twice 适用于任何能响应 * 的对象。只有在调用运行时,才会发现参数数量或类型错误。C++ 函数签名会进行静态检查。C++ 函数声明会确定参数和返回类型,非 void 函数通过显式的 return 语句返回值。返回值不会自动打印。

    Ruby
    1def 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}
  6. 流与精确解析

    Ruby 提供了两种转换方式:Integer(text) 会引发 ArgumentError,除非整个字符串都是数字(或者在使用 exception: false 时返回 nil);而 String#to_i 会读取开头的数字前缀,如果没有数字,则静默返回 0。C++ 流会在流状态中表示提取失败,而且和 to_i 一样,>> 遇到第一个不能属于数字的字符时就会停止。C++ 流提取会通过流状态报告是否成功。如果契约要求输入必须完全匹配,请验证转换结果,并拒绝意外的尾随标记。

    Ruby
    1word, 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 道测验题

更多面向 Ruby 开发者的课程

从其他语言出发学习 C++

常见问题

面向 Ruby 开发者的 C++ 适合谁学习?

适合已经会写 Ruby、想学 C++ 的人。每节课都从你熟悉的 Ruby 代码出发,展示 C++ 如何实现同样的事情,所以你可以跳过入门基础。

需要先会 C++ 吗?

不需要。课程从你的第一个 C++ 程序开始。你需要掌握 Ruby 基础:变量、条件、循环、函数和集合。

面向 Ruby 开发者的 C++ 有多少节课?

面向 Ruby 开发者的 C++ 包含 48 节互动课,你可以按自己的节奏完成。

面向 Ruby 开发者的 C++ 包含哪些内容?

本课程分为以下几章:C++ 文件与值, 条件与循环, 标准容器, 副本与算法, and more。

面向 Ruby 开发者的 C++ 免费吗?

是的。Coddy 上的每门课程都可以免费开始。你可以直接在浏览器中完成课程,无需下载,也无需注册。

Coddy 编程语言插图

免费开始学习 面向 Ruby 开发者的 C++

开始