C++ std-11 常用方法

  1. 对多个值取最值
    C++标准库提供了获取最大值和最小值的方法:
    int mi = std::min(x1, x2);
    int ma = std::max(x1, x2);

如果想获取超过两个数的最值呢?
可以使用嵌套的方法,比如这样:
int mi = std::min(x1, std::min(x2, std::min(x3, x4)));

这样写有点麻烦,下面是更简单的方法:
int mi = std::min({x1, x2, x3, x4})

  1. 万能头文件
    使用 C++ 进行开发时,每用到一个库函数,就需要在代码中 include 该库函数的头文件,比如 string, vector, fstream, thread, list, algorithm......
    有时候光头文件就占了十多行,看起来比较冗长,这里有个万能头文件 bits/stdc++.h,它基本上包括所有的 C++ 标准库,所以在代码中只需要包含这一个头文件即可。
    #include <bits/stdc++.h>

3.auto 关键字
使用 auto 关键字可以让编译器在编译时自动推导出变量的数据类型,而不需要你手动指定。
还有使用容器的场景,比如 map<string, vector<pair<int, int>>> 之类的数据类型,使用 auto 就非常方便。

4.Lambda 表达式
C++11 引入了 Lambda 表达式,可以实现匿名函数,一种没有函数名的函数对象,并且它基于一些简洁的语法可以在各种其他作用域内捕获变量。
如果你需要在代码内部完成一些小的快速操作,没有必要为此编写一个单独的函数,就可以使用 Lambda,一个很常用的场景是将它用在排序函数中。
在使用 sort 函数时,想自定义排序规则,则需要编写一个比较器函数来实现该规则,再将该函数传入 sort 函数中。
比如根据数字的个位数进行排序:

#include <vector>
#include <algorithm>
bool cmp(int a, int b){
    // 按个位数从大到小排序
    return (a % 10) > (b % 10);
}
int main() {
    std::vector<int> v;
    v = {1, 53, 17, 25};
    // sort
    std::sort(v.begin(), v.end(), cmp);
}
// output: [17 25 53 1]

通过 Lambda 表达式,就不用显式编写 cmp 函数。
std::sort(v.begin(), v.end(), [](int a,int b){return (a % 10) > (b % 10);});

可能你还不太会使用 Lambda,基本形式如下:
[](argument1,argument2,.....){//code}

在 () 中传入参数,在 {} 中编写代码,[] 是一个捕获列表,可以指定外部作用域中,可以使用的局部变量:

  • [] — 捕获列表为空,表示在 lambda 表达式中不能使用任何外部作用域的局部变量,只能使用传进去的参数。
  • [=] — 表示按照值传递的方法捕获父作用域的所有变量。
  • [&] — 表示按照引用传递的方法捕获父作用域的所有变量。
  • [this] — 在成员函数中,按照值传递的方法捕获 this 指针。
  • [a, &b] — 不建议直接使用 [=] 或 [&] 捕获所有变量,可以按需显式捕获,就是在 [] 指定变量名,[a] 表示值传递,[&b] 表示引用传递。

5.用 emplace_back 代替 push_back
在 C++11 中,emplace_back 的用法功能与 push_back 类似,都是在向量的末尾添加元素。
但 emplace_back 比 push_back 更有效率,因为 push_back 会先创建一个临时变量(构造函数),然后将其拷贝到 vector 的末尾(拷贝构造函数)。但 emplace_back 就直接在 vector 的末尾构造值(构造函数),少调用了一次拷贝构造函数。
两者的用法相同:

#include <vector>
std::vector<int> v;
v.push_back(0);
v.emplace_back(1);

6.使用 tuple
对于两个不同类型的元素可以用:

std::pair<int, char> pi = std::make_pair(1, 'a');
std::cout << pi.first << " " << pi.second << std::endl;

如果有超过两个元素,且数据类型不同,可以使用 pair 的嵌套,使用时需要不断地调用 first 和 second。

std::pair<int, std::pair<char, std::string>> pi = std::make_pair(1, std::make_pair('a', "he"));
std::cout << pi.first << " " << pi.second.first << std::endl;

这种用法比较繁琐,也可以使用 tuple 进行简化,tuple 是各种数据类型的集合,相当于一个形式更简单的结构体。

#include <tuple>
std::tuple<int, char, std::string> tp = std::make_tuple(1, 'a', "he");
std::cout << std::get<0>(tp) << "," << std::get<1>(tp) << "," << std::get<2>(tp) << std::endl;

7.for 循环的简便写法
当遍历 vector 中每个元素时,我之前的写法:

std::vector<int> s = {8, 2, 3, 1};
for (std::vector<int>::iterator it = s.begin(); it != s.end(); ++it) {
    std::cout << *it << ' ';
}

C++11 的 for 循环支持下面这种写法:

std::vector<int> v = {8, 2, 3, 1};
for (int i: v) {
    std::cout << i << ' ';
}

其中 i 就是 vector 中的每个元素,还可以配合 auto 关键字,食用更佳。

8.**不用 for 循环,计算一个数字的位数 **

#include <iostream>
#include <cmath>
int main() {
    // cmath
    int N=105050;
    int n= std::floor(std::log10(N))+1; 
    std::cout << n;
    return 0;
}

9.不用 for 循环,对数组进行深拷贝
使用 copy_n 函数可以从源容器复制指定个数的元素到目的容器中。

#include <algorithm>
int x[3]={1,2,3}; 
int y[3]; 
std::copy_n(x, 3, y);

copy_n 有三个参数:

  • 第一个参数是指向第一个源元素的输入迭代器;
  • 第二个参数是需要复制的元素的个数;
  • 第三个参数是指向目的容器的第一个位置的迭代器。

10.all_of, any_of, none_of
在 Python 中,有 all() 和 any() 来判断列表中的元素是否都满足条件,或者至少一个元素满足条件。
C++ 也能实现类似的功能,就是使用 all_of,any_of,none_of。这里需要用到前面提到的 Lambda 表达式。
判断数组所有元素是否都为正(all_of):

#include <algorithm>
#include <iostream>

int main() {
    int ar[6] = {1, 2, 3, 4, 5, -6}; 

    std::all_of(ar, ar+6, [](int x){ return x>0; })? 
    std::cout << "All are positive elements" : 
    std::cout << "All are not positive elements"; 
    return 0; 
}

判断数组是否至少有一个元素为正(any_of):

std::any_of(ar, ar+6, [](int x) { return x>0; })? 
std::cout << "At least one are positive elements" : 
std::cout << "all are not positive elements"; 

判断是否没有元素为正(none_of):

std::none_of(ar, ar+6, [](int x) { return x>0; })? 
std::cout << "all are not positive elements" : 
std::cout << "At least one are positive elements"; 

11.获取元祖多个值
在 Python 中,可以使用下面方法同时获取多个值:

arr = (1, 2.0, "3")
a, b, c = arr

现在 C++11 也可以实现这个功能了,用到了 std::tie:

#include <string>
#include <tuple>
#include <iostream>

int main() {
    std::tuple<int, double, std::string> t0 = {1, 2.0, "3"};
    int i = 0; double d = 0.; std::string s = "";
    std::tie(i, d, s) = t0;
    std::cout << i << " " << d << " " << s << std::endl;
    return 0; 
}

代码中定义了元祖 t0,通过 std::tie(i, d, s)= t0; 同时获取了元祖中的三个值。
有时候想只获取元祖中的部分值,则需要用到 std::ignore, 比如只获取前两个值:
std::tie(i, d, td::ignore) = t0;
std::tie 不但可用于 tuple,还能用于 pair。

12.数组去重
在 python 中对列表去重,有一个简单方法,就是将列表转换为集合 set,其实 C++ 也有类似的操作。

#include <set>
#include <vector>

int main()
{
    std::vector<int> vec;
    vec = {1, 2, 4, 5, 1, 6, 1, 2};
    std::set<int> s(vec.begin(), vec.end());
    vec.assign(s.begin(), s.end());
    return 0;
}
posted @ 2021-03-02 11:52  Xu_Lin  阅读(294)  评论(0编辑  收藏  举报