C++ 11 支持
1. 通过一对 {} 将值分配给容器
有选手这样写
pair<int, int> p;
p = make_pair(3, 4);
实际上可以这样写
pair<int, int> p;
p = {3, 4};
更复杂的也可以
pair<int, pair<char, long long> > p;
p = {3, {'a', 8ll}};
vector
vector<int> v;
v = {1, 2, 5, 2};
for (auto i: v)
cout << i << ' ';
cout << '\n';
// prints "1 2 5 2"
deque
deque<vector<pair<int, int>>> d;
d = {{{3, 4}, {5, 6}}, {{1, 2}, {3, 4}}};
for (auto i: d) {
for (auto j: i)
cout << j.first << ' ' << j.second << '\n';
cout << "-\n";
}
// prints "3 4
// 5 6
// -
// 1 2
// 3 4
// -"
set
set<int> s;
s = {4, 6, 2, 7, 4};
for (auto i: s)
cout << i << ' ';
cout << '\n';
// prints "2 4 6 7"
list
list<int> l;
l = {5, 6, 9, 1};
for (auto i: l)
cout << i << ' ';
cout << '\n';
// prints "5 6 9 1"
array
array<int, 4> a;
a = {5, 8, 9, 2};
for (auto i: a)
cout << i << ' ';
cout << '\n';
// prints "5 8 9 2"
tuple
tuple<int, int, char> t;
t = {3, 4, 'f'};
cout << get<2>(t) << '\n';
注意不适用于 stack 和 queue。
宏中参数的名称
您可以使用“#”符号来获取传递给宏的参数的确切名称:
#define what_is(x) cerr << #x << " is " << x << endl;
// ...
int a_variable = 376;
what_is(a_variable);
// prints "a_variable is 376"
what_is(a_variable * 2 + 1)
// prints "a_variable * 2 + 1 is 753"
隐藏功能(不是真的隐藏但不经常使用)
__gcd(value1, value2)
你不需要为gcd函数编写欧几里得算法,从现在开始我们可以使用了。此函数返回两个数字的 gcd。
__builtin_ffs(x)
此函数返回 x 的最低位的 的位置+1。 比如说因为 10 的二进制为 1010, __builtin_ffs(10) 返回 ,因为最低位的1在第1位,加上1以后返回 2。
__builtin_clz(x)
__builtin_ctz(x)
__builtin_popcount(x)
可变参数函数和宏
基于范围的 for 循环
set<int> s = {8, 2, 3, 1};
for (auto it: s)
cout << it << ' ';
// prints "1 2 3 8"
如果需要更改值
vector<int> v = {8, 2, 3, 1};
for (auto &it: v)
it *= 2;
for (auto it: v)
cout << it << ' ';
// prints "16 4 6 2"
Lambda 表达式
定义如下:
[capture list](parameters) -> return value { body }
例如
auto f = [] (int a, int b) -> int { return a + b; };
cout << f(1, 2); // prints "3"
例如
vector<int> v = {3, 1, 2, 1, 8};
sort(begin(v), end(v), [] (int a, int b) { return a > b; });
for (auto i: v) cout << i << ' '; // print "8 3 2 1 1 "
C++ Lambda 捕获列表
在 C++ 中,lambda 表达式允许捕获外部变量以便在其体内使用。捕获列表是 lambda 的第一个部分,用方括号 []
包围,包含如何捕获外部变量的信息。
捕获方式
-
按值捕获
- 语法:
[x]
或[=]
- 说明:捕获外部变量的副本。
- 示例:
int x = 10; auto lambda = [x]() { return x + 1; }; // 捕获 x 的副本
- 语法:
-
按引用捕获
- 语法:
[&x]
或[&]
- 说明:捕获外部变量的引用。
- 示例:
int x = 10; auto lambda = [&x]() { x++; }; // 捕获 x 的引用
- 语法:
-
混合捕获
- 语法:
[=, &x]
或[&, y]
- 说明:可以混合使用按值和按引用捕获。
- 示例:
int x = 10, y = 20; auto lambda = [=, &x]() { x += y; }; // y 按值捕获,x 按引用捕获
- 语法:
捕获所有变量
- 使用
[]
捕获列表来捕获所有变量。- 语法:
[=]
(按值)或[&]
(按引用)。 - 示例:
int x = 10, y = 20; auto lambda = [=]() { return x + y; }; // 所有变量按值捕获
- 语法:
捕获列表中的常量
- 可以捕获常量:
- 示例:
const int z = 30; auto lambda = [z]() { return z * 2; }; // 捕获常量 z
- 示例:
注意事项
- 捕获列表的顺序会影响捕获的方式(值或引用)。
- 如果 lambda 在定义的作用域之外被调用,确保引用变量的生命周期足够长。
原始字符串
可以在代码里嵌入一段原始字符串,该原始字符串不作任何转义。
string str1 = "a\nb\nc\n";
cout << str1 <<endl;
cout << "---------------------" <<endl;
string raw_str = R"(a\nb\nc\n)";
cout << raw_str <<endl;
cout << "---------------------" <<endl;
string raw_str2 = R"(x
y
z)";
cout << raw_str2 << endl;
输出结果为
a
b
c
---------------------
a\nb\nc\n
---------------------
x
y
z
merge
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
//容器元素合并,并存储到另一个容器中
//注意:两个容器必须是有序的
//beg1 容器1开始迭代器
//end1 容器1结束迭代器
//beg2 容器2开始迭代器
//end2 容器2结束迭代器
//dest 目标容器开始迭代器
正则表达式
C++14 支持
数字
现在,您可以使用单引号 ( ' ) 分隔数字的数字,以提高可读性
auto num = 10'000'000; // 整数, num = 10^7
auto fnum = 0.000'015'3; // 浮点数, fnum = 1.53 * 10^-5
auto bnum = 0b0100'1100'0110; // 二进制数,bnum = (010011000110)2 = (4C6)16 = 1222