- 侯捷c++11&14新标准
- 引言
- variadic templates
- Spaces in Template Expressions
- nullptr
- auto
- 初始化(initializer_list<>)
- explicit(for ctors taking more than one argument)
- Range-based for statement
- 全局函数 begin() end()
- =default =delete
- Alias Template 化名
- template template parameter
- Type Alias, noexcept, override, final
- Type Alias
- noexcept
- override
- final
- decltype
- Lambdas
- Variadic Templates
- 标准库
- Rvalue reference
- array forward_list
- Hashtable
- Hash function
- tuple
新特性包括语言和标准库两个部分
标准库是以头文件的形式呈现(不加.h)
cplusplus.com
variadic templates Spaces in Template Expressions nullptrnullptr代替 0或者NULL
auto不确定变量类型 让编译器自己推
比如:代替迭代器、lambda表达式
初始化(initializer_list<>)变量的后面直接放大括号
int values[]{1, 2, 3}
initializer_list val {?, ?, ?, …} 用一个array来接收 其实是保存头指针
cout << max({ 1, 2, 3 }) << " " << min({string(“a”), string(“b”), string(“c”) }) << endl;
explicit(for ctors taking more than one argument)2.0之前让编译器不要自作主张
#includeRange-based for statementusing namespace std; class A { public: //这里用explicit关键词来修饰类构造函数. explicit A(int i = 5, int j = 10) { m_a = i; m_b = j; } private: int m_a; int m_b; }; int main() { A s; //这样直接赋值,会被提示错误,因为explicit抑制隐式转换的进行 s = 10;//这样会报错!!! //当然显示转换还是可以的. s = A(20); system("pause"); return 0; }
for(dec1 : col){
statement
}
全局函数 begin() end()
e.g. begin(vector…) 相当于 vector.begin()
=default =delete=default 表示子类直接继承父类
=delete 表示子类不写这个函数
有指针的类就有big three(即默认构造,拷贝构造,析构)
Alias Template 化名templatetemplate template parameterusing Vec = vector >; Vec coll; 等价于 vector > coll;
template
templateType Alias, noexcept, override, final Type Aliastypename Container> class XCls{ private: container c; }; template class test{ private: T t; }; template using mlst = list >;// Alias Template 化名 int main(){ XCls myContain; // XCls mylist; // 错误 因为list中有两个参数 XCls mylist; }
using func = void (*)(int, int); //等价于 typedef void (*func)(int, int);noexcept
void foo() noexcept(true){}// 不抛出异常
// move过程中不抛出异常,让程序放心。有move的时候程序有限调用move 而不是copy construct
MyString(MyString&& str) noexcept{}
MyString& operator=(MyString&& str) noexcept{}
override
帮助编译器检查是否为重写
finalclass Base1 final{};// 没有人能继承Base1
virtural void f() final{};// 没有人能重写f()
decltype
获得变量类型
mapLambdascoll; decltype(coll)::value_type elem; // 相当于map ::value_type elem; Template auto add(T1 a, T2, b) -> decltype(a + b)
[&id]()mutable{++id;}// 才能改变id值
int tobefound = 5;
auto lambda1 = [tobefound](int val) {return val == tobefound; };
cout << lambda1(5) << " " << lambda1(10) << endl;
属于inline内联函数
Variadic Templates递归
void func(){}
template
void func(const T& firstArg, const Types& ...args){
func(args...);
}
template标准库 Rvalue referenceclass tuple{ public: size_t seed = 0; tuple(const Value&... args){tuple(seed, args...)} }; // 特化 template<> class tuple<>{}; template class tuple:private tuple { typedef tuple inherited; protect: Head m_head; public: tuple(){}; tuple(Head v, Tail... vtail):m_head(v), inherited(vtail){} // typename Head::type head(){return m_head}; auto head()->decltype(m_head){return m_head}; inherited& tail(){return* this;} }
copy 和 move
insert(cons_iterator _position, const value_type& _x); insert(cons_iterator _position, const value_type&& _x); 关键字 move forward才能完美转移array forward_list
array数组 int a[10]; int* a = new int [10]
forward_list就是list改成单项列表
Hashtable Hash function tupletuplet1(41, 6.3, "nice"); get<0>(t1); get<1>(t1); get<2>(t1); auto t2 = make_tuple(41, 6.3, "nice"); get<1>(t1) = get<>(t2); t1 = t2;



