Python与C++类型对照及常用操作
基础类型
Python类型 | C++对应类型 | 简单示例对比 | 头文件需求 |
---|---|---|---|
int | int /int64_t | Py: a = 42 C++: int a = 42; | 无 |
float | double | Py: b = 3.14 C++: double b = 3.14; | 无 |
bool | bool | Py: c = True C++: bool c = true; | 无 |
str | std::string | Py: d = "你好" C++: std::string d = "你好"; | <string> |
bytes | vector<uint8_t> | Py: data = b"hello" C++: vector<uint8_t> data = {'h','e','l','l','o'}; | <vector> |
None | nullptr | Py: empty = None C++: auto empty = nullptr; | 无 |
容器类型对照
- 列表(list ⇄ vector)
# Python列表操作
lst = [1, 2, 3]
lst.append(4) # [1,2,3,4]
lst.pop() # [1,2,3]
print(lst[1]) # 2
print(len(lst)) # 3
// C++ vector操作
#include <vector>
#include <iostream>vector<int> lst = {1, 2, 3};
lst.push_back(4); // 添加元素
lst.pop_back(); // 移除末尾
cout << lst[1]; // 访问元素 (2)
cout << lst.size(); // 获取大小// 遍历vector的三种方式:
// 1. 传统for
for(size_t i=0; i<lst.size(); ++i) {cout << lst[i] << " ";
}// 2. 范围for
for(int num : lst) {cout << num << " ";
}// 3. 使用迭代器
for(auto it=lst.begin(); it!=lst.end(); ++it) {cout << *it << " ";
}
- 字典(dict ⇄ unordered_map)
# Python字典操作
d = {"name": "小明", "age": 18}
print(d["name"]) # "小明"
print("age" in d) # True
d["score"] = 90 # 添加键值
for k, v in d.items(): # 遍历print(f"{k}:{v}")
// C++ unordered_map操作
#include <unordered_map>
#include <string>
#include <iostream>unordered_map<string, int> d = {{"name", "小明"},{"age", 18}
};cout << d["name"]; // 访问值
cout << (d.count("age")>0); // 检查键是否存在
d["score"] = 90; // 添加/修改键值// 遍历map的两种方式:
// 1. 使用auto&
for(auto& pair : d) {cout << pair.first << ":" << pair.second << endl;
}
- 集合(set ⇄ unordered_set)
# Python集合操作
s = {1, 2, 2, 3} # {1,2,3}
s.add(4) # {1,2,3,4}
print(2 in s) # True
// C++ unordered_set操作
#include <unordered_set>
#include <iostream>unordered_set<int> s = {1, 2, 3};
s.insert(4); // 添加元素
cout << (s.count(2)>0); // 检查存在性// 遍历集合
for(int num : s) {cout << num << " ";
}
常用操作
- 简化打印函数
#include <iostream>
#include <vector>
#include <unordered_map>// 打印vector
template<typename T>
void print(const vector<T>& vec) {for(const auto& item : vec) {cout << item << " ";}cout << endl;
}// 打印map
template<typename K, typename V>
void print(const unordered_map<K,V>& map) {for(const auto& pair : map) {cout << pair.first << ":" << pair.second << " ";}cout << endl;
}// 使用示例
vector<int> nums = {1, 2, 3};
unordered_map<string, int> dict = {{"a",1}, {"b",2}};
print(nums); // 输出: 1 2 3
print(dict); // 输出: a:1 b:2
- range模拟器
// 实现Python风格的range
vector<int> range(int stop) {vector<int> result;for(int i=0; i<stop; ++i) {result.push_back(i);}return result;
}vector<int> range(int start, int stop) {vector<int> result;for(int i=start; i<stop; ++i) {result.push_back(i);}return result;
}// 使用示例
for(int i : range(5)) {cout << i << " "; // 0 1 2 3 4
}
- 文件操作对比
# Python文件操作
with open("data.txt", "w") as f:f.write("Hello\nWorld")with open("data.txt", "r") as f:for line in f:print(line.strip())
// C++文件操作
#include <fstream>
#include <string>
#include <iostream>// 写入文件
ofstream out("data.txt");
out << "Hello\nWorld";
out.close();// 读取文件
ifstream in("data.txt");
string line;
while(getline(in, line)) {cout << line << endl;
}
in.close();
备注
有问题随时交流~~