C++标准11
1.设置编译器的开关支持c++11
2.数量不定的模板,可以递归使用
#include <iostream>
using namespace std;
void print() {
}
template<typename T,typename... Types>
void print(const T& firstArg, const Types&... args) {
cout << firstArg << endl;
print(args...);
}
void Variadic_Template() {
print(7.5, "hello", 42);
}
int main() {
Variadic_Template();
return 0;
}
3. nullprt and std::nullprt_t
void f(int)
void f(void*);
f(0)
f(NULL)
f(nullprt)
4.auto
vectorv;
auto pos=v.begin();
5.initialization
int value[] {1,3,4};
initializer array<T,n>
6.initializer_list
int i //i has undefined value
int j{}; //initialized 0
int *q{}; //initialized by nullptr
initializer_list<>
void print(initializer_list<int>vals) {
for (auto p = vals.begin(); p != vals.end(); p++) {
cout << *p << endl;
}
}
print({12, 3, 5, 7, 11, 13, 17});
vectorv3;
v3={2,4,5,7,87,8,9};//调用如下标准库函数
vector& operator=(initializer_list<_Ty> _Ilist)
{ // assign initializer_list
_Assign_range(_Ilist.begin(), _Ilist.end(), random_access_iterator_tag{});
return (*this);
}
7.explicit
显示声明
#include<iostream>
using namespace std;
struct Complex {
int real,imag;
Complex(int re,int im=0):real(re),imag(im){}
//如果加explict即不可以调用
Complex operator+(const Complex& x) {
return Complex((real + x.real), (imag + x.imag));
}
};
void test1() {
Complex c1(13);
Complex c2 = c1 + 5;
cout << c1.real << " " << c1.imag << endl;
cout << c2.real << " " << c2.imag << endl;
}
int main() {
test1();
return 0;
}
8.range-based for statement
for(decl:coll){
statement
}
for(int i:{2,3,4,5,6,7}){
cout<<i<<endl;
}
vector<double>vec;
for(auto elem:vec){
cout<<elem<<endl;
}
9.=default,=delete
=0只能用于虚函数 virtual
class Empty {
public:
1--Empty() {}
2--Empty(const Empty& rhs) {}
3--Empty& operator=(const Empty& rhs) {
cout << "copy asign";
return *this;
};
};
Empty e1;//调用1
Empty e2 = e1;//调用2
Empty e3;
e3 = e1;//调用3
struct Nodtor{
Nodtor()=default;
}
Nodtor *p=new Nodtor();
delete p;
10.alias
函数名称即函数指针
using func=void(*)(int int)
void example(int,int){}
func fn=example;
11.noexcept,override,final
void foo()noexcept //函数没有异常
//通知c++ move不会剖出异常,并且先与标准库执行
excepting 异常
override防止继承时使用虚函数,函数名错误,导致产生新的函数
final 表明已经是最后一个类,不能被继承了
12.decltype
1.指示函数返回值
template<typename T1,typename T2>
auto add(T1 x,T2 y)->decltype(x+y)
2.应用元编程
3.应用在lambda表达式
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
cout << *it << " ";
}
// 定义lambda函数,lambda函数作为变量的变量类型较复杂,因此使用auto进行推断
auto cmp = [](const Person &p1, const Person &p2) {
return p1.lastname() < p2.lastname() ||
(p1.lastname() == p2.lastname() && p1.firstname() < p2.firstname());
};
// 使用decltype语法推断lambda函数cmp的类型
std::set<Person, decltype(cmp)> coll(cmp);
13.lambdas
class UnNameLocalFunction {
int localVar;
public:
UnNameLocalFunction(int var):localVar(var){}
bool operator()(int var) {
return var == localVar;
}
};
void test3() {
int tobefound = 5;
UnNameLocalFunction lambda2(tobefound);
//lambdas表达式
auto lambda1 = [tobefound](int var) {return var == tobefound; };
cout << lambda1(5) << endl;
cout << lambda2(5) << endl;
}
14.Variadic Templates
#include<iostream>
using namespace std;
void printf1(const char* s) {
if (*s == '%' && *(++s) != '%') {
throw std::runtime_error("invalid format:missing arguments");
}
cout << *s++;
}
template<typename T,typename...Args>
void printf1(const char* s, T value, Args...args) {
while (*s) {
if (*s == '%'&& *(++s)!= '%') {
cout << value;
printf1(++s, args...);
return;
}
}
}
void test1() {
int* pi = new int;
printf1("%d%s%p%f\n", 15, "this is ace",pi, 3.1415926);
}
int main() {
test1();
return 0;
}
15.Rvalue references(右值引用)
目的:解决非必要的拷贝
1.临时变量是一种右值
2.右值不可以放在左边
3.函数返回值是右值,右值不可以取地址