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 + ")");
}