C++标准11

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.函数返回值是右值,右值不可以取地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex1_Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值