c++的深入学习

c++输入输出提速

  1. std::cin.tie(nullptr); 解除的是C++运行库层面的对数据传输的绑定
  2. std::sync_with_stdio(false); 这个函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑在了一起。

以上参考自:https://www.cnblogs.com/letgo/p/5631247.html

c++11特性

  1. auto 用于从初始化表达式中推断出变量的数据类型。因此,auto定义的变量必须有初始值。 auto在c++14中可以作为函数返回值

  2. decltype 的作用是选择并返回操作数的数据类型

  3. nullptr 是为了解决原来C++标准中NULL的二义性问题而引进的一种新的类型,因为NULL实际上代表的是0。

  4. constexpr 类型以便由编译器来验证变量的值是否为一个常量表达式,必须在编译期间计算出它的值并且它的值不可以被改变。const只保证了运行时不直接被修改(但这个东西仍然可能是个动态变量),const修饰的是类型,constexpr修饰的是用来算出值的那段代码

  5. stl容器 array 跟数组并没有太大区别,相对于数组,增加了迭代器等函数。

  6. stl容器 unordered_map 与std::map用法基本差不多,但STL在内部实现上有很大不同,std::map使用的数据结构为二叉树,而std::unordered_map内部是哈希表的实现方式,哈希map理论上查找效率为O(1)。但在存储效率上,哈希map需要增加哈希表的内存开销。

  7. stl容器 forward_list

  8. stl容器 unordered_set

  9. lambda 表达式形式
    [capture](parameters)->return-type{body}();

    [] 不捕获任何变量
    [&] 以引用方式捕获所有变量
    [=] 用值的方式捕获所有变量(可能被编译器优化为const &)
    [=, &foo] 以引用捕获foo, 但其余变量都靠值捕获
    [&, foo] 以值捕获foo, 但其余变量都靠引用捕获
    [bar] 以值方式捕获bar; 不捕获其它变量
    [this] 捕获所在类的this指针

  10. 基于范围的for循环,支持数组和容器

  11. 元组类型 tuple
    详细介绍看这里

  12. emplace 很重要
    使用mplace_back替代push_back()可以在这上面有进一步优化空间,只调用构造函数不需要调用右值引用转移构造函数

以上参考自:https://blog.csdn.net/raiven2008/article/details/82114736
https://www.cnblogs.com/chengjundu/p/10893702.html
https://blog.csdn.net/a379039233/article/details/83714770

c++14

  1. auto 可以作为函数返回值
  2. C++11的constexpr函数只能包含一个表达式,C++14放松了这些限制,支持诸如if 和switch等条件语句,支持循环,其中包括基于区间(range)的for 循环
  3. hash模板

以上参考自:http://c.biancheng.net/view/523.html

常用函数

  1. sort 不稳定排序
  2. stable_sort 稳定排序
  3. partial_sort 部分排序,前k个有序的
vector<int> v{5,6,23,6,1,3,2,7,0};
int k=4;
partial_sort(v.begin(),v.begin()+k,v.end());
for(auto i:v)
	cout << i << " ";
  1. nth_elemen 找到第k个元素,前k-1个小于k,后边的大于k,但不一定是有序的
vector<int> v{22, 7, 93, 45, 19, 56, 88, 12, 8, 7, 15, 10};
	int k=4;
	nth_element(begin(v),begin(v)+k,end(v));
	for(auto i:v)
		cout << i << " ";
  1. max_element 和 min_element 和 minmax_element 获取容器最大最小值
  2. binary_search 二分查找,返回 bool
  3. lower_bound 和 upper_bound 查找第一个大于等于 , 查找第一个大于
  4. equal_range 返回的pair对象
  5. all_of any_of none_of
  6. count 和 count_if 计算有多少个等于给定值的
  7. equal 两个容器是否相等,长度不一样总返回false
vector<int> v{22, 7, 93, 45, 19, 56, 88, 12, 8, 7, 15, 10};
vector<int> w{22, 7, 93, 45, 19, 56, 88, 12, 8, 7, 15, 10};
cout << equal(begin(v)+1,end(v),w.begin(),w.end()) << endl;
cout << equal(begin(v)+1,end(v),w.begin()) << endl;
  1. next_permutation 和 prev_permutation , 可以自定义比较方式
  2. unique
  3. rotate
  4. reverse
  5. swap
  6. fill
  7. accumulate
  8. distance
  9. next
  10. replace和 replace_if

其他

  1. inline 内联,防止频繁调用函数,导致可能的爆栈
    https://www.runoob.com/w3cnote/cpp-inline-usage.html
  2. 开启O2优化,俗称加氧
    #pragma GCC optimize(2)
  3. 复数 complex
complex<double> z{1.5, -2.5}; // z: 1.5 - 2.5i
z.imag(99); // z: 1.5 + 99.0i
z.real(-4.5); // z: -4.5 + 99.0i
std::cout << "Real part: " << z.real()<< " Imaginary part: " << z.imag()<< std::endl;
  1. string 的坑点
//res = res + curr+to_string(count);//超出内存限制
 res += curr+to_string(count);  //这样才能通过

原因是: res+= s是表示在原来字符串后添加字符串。 res = res + s 是开辟了一个新内存存放res+s。如果字符串太长,开辟新空间就需要消耗大量内存。

发布了33 篇原创文章 · 获赞 26 · 访问量 6347

猜你喜欢

转载自blog.csdn.net/qq_41829380/article/details/104863538
今日推荐