VS2010&&c++11


1、智能指针

#include <memory>
std::shared_ptr<int> p1 = std::make_shared<int>(2);//为对象和引用计数都分配了内存空间
std::shared_ptr<int> pvec(new int[10],std::default_delete<int[]>());//数组需使用删除器,不可使用[]操作符

std::weak_ptr<double> w3(p1);

std::unique_ptr<int> p3(new int(2));
std::unique_ptr<int []> pVec(new int[5]());//使用默认删除器,可使用[]操作符

#include<iostream>
#include<vector>
#include<map>
#include<unordered_map>
#include<string>
#include<memory>
#include<functional>

struct DIpoint
{
	int pointno;
	int rtuno;
	int code_bit;

	DIpoint()
	{
		pointno = -1;
		rtuno = -1;
		code_bit = -1;
	}

	bool operator < (const DIpoint& obj)const
	{
		if(pointno < obj.pointno)
		{
			return true;
		}

		return false;
	}
};

class base
{
public:
	base()
	{
		std::cout<<"base()"<<std::endl;
	};
	base(const base &obj)
	{
		std::cout<<"base(const )"<<std::endl;
	}
	~base()
	{
		//std::cout<<"~ base()"<<std::endl;
	};

	base(base &obj)
	{
		std::cout<<"base copy"<<std::endl;
	}
	//移动拷贝构造
	base(base &&obj)
	{
		std::cout<<"base &&"<<std::endl;
	}

	base& operator=(base &obj)
	{
		std::cout<<"base ="<<std::endl;
		return *this;
	}
	//移动赋值
	base& operator=(base &&obj)
	{
		std::cout<<"base = &&"<<std::endl;
		return *this;
	}

private:

};

void stdptr()
{
	std::shared_ptr<int> p1 = std::make_shared<int>(2);//为对象和引用计数都分配了内存空间
	std::shared_ptr<int> pvec(new int[10],std::default_delete<int[]>());
	int b = *p1;
	std::cout<<p1<<std::endl;

	std::unique_ptr<int> p3(new int(2));
	std::unique_ptr<int []> pVe(new int[5]());
	pVe[0] = 1;
	pVe[1] = 2;
	std::cout<<pVe[0]<<std::endl;

	std::vector<std::unique_ptr<base[]>> pve;
	std::unique_ptr<base[]> pveb(new base[5]());
	base A;
	pveb[0] = A;
	pve.push_back(std::move(pveb));

	std::unique_ptr<char[]> tmp(new char[6]());
	char* buf = "hello";
	memcpy(tmp.get(),buf,6);
	char buff[6];
	memcpy(buff,tmp.get(),6);


	std::map<int, std::unique_ptr<std::vector<base>>> box;
	std::unique_ptr<std::vector<base>> vec(new std::vector<base>());
	box.insert(std::make_pair(1,std::move(vec)));
	base B;
	box[1]->push_back(B);

	std::unordered_map< int, std::unique_ptr<std::vector<DIpoint>> > m_DIpoint_box;
	std::unique_ptr<std::vector<DIpoint>> ptr(new std::vector<DIpoint>);
	m_DIpoint_box.insert(std::make_pair(1,std::move(ptr)));
}

2、auto类型推导

std::vector<int> box;
auto it = box.begin();

3、lambda表达式

[capture list] (parameter list) -> return type { function body }
接收参数结果 = [ ] ( 参数列表 ) { return 返回表达式参数 } ( 实际透传的参数 )

  • Capture list:捕获列表,Lambda函数中定义的局部变量列表,可以为空。
  • Parameter list:参数列表
  • function body:函数体
  • return type:尾置类型指明,一般情况可以没有,编译器会自动推导出返回类型。当函数体有多个返回值时,编译会产生错误,需要指明返回类型。
string strRet = [](const string& str) { return "Hello from " + str; }("Lambda");

auto fun = [](const string& str)->string { return "Hello from " + str; };
strRet = fun("Lambda");


int Flag = 10;
int Arr[] = {5, 3, 2, 11, 4, 22};
auto first = find_if(Arr, Arr+6, [Flag](int Value){return Value > Flag;});

4、for_each

void print(int val)
{
	cout<< val <<endl;
}
auto print = [](const auto& x)  // 定义一个lambda表达式
{
    cout << x << ",";
};

vector<int> v;
for_each(v.begin(),v.end(), print);// for_each算法

5、正则表达式

std::regex

6、bind函数模板

#include <functional>
void f(int n1 ,int n2,int n3 ,int n4 ,int n5){
    cout << n1 <<" "<< n2 <<" "<<n3 <<" "<<n4<<" "<<n5<<'\n';
}

using namespace std::placeholders;
auto b = bind(f,2,_1,_2,4,5);//(f函数,值,占位符,占位符,值,值)
b(100,200);

7、&&右值引用

//移动构造函数和移动赋值函数
std::move()
std::forward//完美转发
base fun( base &a)
{
	base B;
	//B = a;
	return B;
}

typedef int (*Func)(int,int);

int CallBack(Func pInt, int a, int b) { return pInt(a, b); }

int add(int a, int b){ return a+b; }

int main()
{
	int res = CallBack(&add,3,5);

	Func f = &add;
	res = CallBack(f,3,6);
	res = f(3,6);

	stdptr();

	std::vector<base> vec;
	base A;
	//使用移动拷贝构造
	vec.push_back(std::move(A));

	std::map<int, base> mp;
	//使用移动拷贝构造
	mp.insert(std::make_pair(1,std::move(A)));

	//移动赋值
	base B;
	B = std::move(A);
	B = fun(A);

	//直接引用
	base&& C = fun(A);

	//string的右值引用
	std::string buf = "hello";
	std::string buff = std::move(buf);
	std::vector<std::string> ve;
	ve.push_back(std::move(buff));

	std::cout<<buf<<std::endl;
	std::cout<<buff<<std::endl;


	std::string bu = std::move("(" + buf + ")");
}

8、VS2010栈大小设置

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值