关于C++标准库
标准库里提供的,我们可以直接使用的就是数据结构(容器)和算法。
推荐书籍:Algorithms + Data Structures = Programs
仿函数Functors → 算法Algorithms → 迭代器Iterators → 容器Containers
都自己用一遍这些容器、算法
如何确定自己的编译器是否支持C++11?
cout << __cplusplus << endl;
C++11输出201103,C++98和C++03的C++输出199711,
__cplusplus 是一个宏
数量不定的模板参数 variadic template (since C++11)
typename... (写的比较好的博客)
#include <iostream>
#include <bitset>
using namespace std;
void print() {
cout << __cplusplus << endl;
}
template<typename T, typename... Types>
void print(const T& firstArg, const Types&... args) {
cout << "firstArg = " << firstArg << " sizeof...(args) = " << sizeof...(args)<< endl;
print(args...); // 有种递归的感觉,但是每次递归的参数类型是变化的
}
int main() {
print(7.5, "hello", bitset<16>(377), 42); //bitset中应该重载<<
return 0;
}
输出:
firstArg = 7.5 sizeof...(args) = 3
firstArg = hello sizeof...(args) = 2
firstArg = 0000000101111001 sizeof...(args) = 1
firstArg = 42 sizeof...(args) = 0
201402
最后一行输出的 201402 是因为调用了print()
sizeof...可以得到pack中有多少个参数
STL中很多地方用typename...翻新了
auto (since C++11)
不赋值的时候不能用auto,编译器无法推断类型。
初学者最好少用auto,要自己清楚自己每个变量的类型。