C++11 新特性

常用新特性汇总

https://zhuanlan.zhihu.com/p/102419965?utm_source=qq

https://blog.csdn.net/jiange_zh/article/details/79356417

C++11 FAQ中文版 

https://wizardforcel.gitbooks.io/cpp-11-faq/49.html

https://legacy.gitbook.com/book/wizardforcel/cpp-11-faq/details

 

智能指针

shared_ptr, make_shared, shared_from_this

https://docs.microsoft.com/en-us/cpp/cpp/smart-pointers-modern-cpp?view=vs-2019

https://docs.microsoft.com/en-us/cpp/cpp/how-to-create-and-use-shared-ptr-instances?view=vs-2019

https://www.cnblogs.com/jiayayao/p/6128877.html

 

c++11中的shared_from_this

https://blog.csdn.net/caoshangpa/article/details/79392878?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

https://zh.cppreference.com/w/cpp/memory/enable_shared_from_this/shared_from_this

 

raw指针需要手动new delete

而智能指针可以自动在不需要时删除对象,不用显示调用delete。

#include 

class A: public std::enable_shared_from_this{

}
 

 

随机数生成

https://www.cnblogs.com/egmkang/archive/2012/09/06/2673253.html

https://en.cppreference.com/w/cpp/numeric/random

设置seed

#include 
#include 
using namespace std;

std::random_device rand_seed;
std::mt19937 mt;
//std::mt19937 mt(rand_seed());

int init_num = 20;

mt.seed(1);

for (int i = 0; i < init_num; i++) {
    cout << dist_fp32(mt) << " ";
}
cout << endl;
mt.seed(rand_seed());
for (int i = 0; i < init_num; i++) {
    cout << dist_fp32(mt) << " ";
}
cout << endl;


多线程

https://blog.csdn.net/ouyangfushu/article/details/80199140

获取多线程返回值:

https://www.cnblogs.com/qicosmos/p/3534211.html

简单范例:

#include 
#include 

std::string simplefunc(std::string a){ 
    return a;
}

int main(){
      auto future = std::async(std::launch::async, simplefunc, "hello world");
      std::string simple = future.get();
      cout<

 

std::launch::async:在调用async就开始创建线程。

std::launch::deferred:延迟加载方式创建线程。调用async时不创建线程,直到调用了future的get或者wait时才创建线程。

 

线程延时

    std::chrono::milliseconds dura(1000); //定一个5秒的时间
    std::this_thread::sleep_for(dura);  //休息一定时常
 

lambda表达式

https://www.cnblogs.com/DswCnblog/p/5629165.html

 

std::bind, std::function

https://en.cppreference.com/w/cpp/utility/functional/bind

ttps://www.jianshu.com/p/f191e88dcc80

https://blog.csdn.net/tennysonsky/article/details/77447804

https://stackoverflow.com/questions/6610046/stdfunction-and-stdbind-what-are-they-and-when-should-they-be-used

基于bind,function和random实现不同随机数生成方式

    int seed = 1;
    float range0 = 0;
    float range1 = 10;

    std::mt19937 mt(seed);
    std::uniform_real_distribution unoform_rand(range0, range1);
    std::normal_distribution<> normal_rand(range0, range1);

    std::function rand_generator = bind(unoform_rand, mt);

    for (int i = 0; i < 10; i++) {
        cout << rand_generator() << " ";
    }
    cout << endl;
    rand_generator = std::bind(normal_rand, mt);

    for (int i = 0; i < 10; i++) {
        cout << rand_generator() << " ";
    }
    cout << endl;

 

谷歌编码风格

https://zh-google-styleguide.readthedocs.io/en/latest/

sublime text 格式化工具

https://github.com/timonwong/SublimeAStyleFormatter

SublimeAStyleFormatter google风格配置

        "style": "google",

       "attach-namespaces": true,

        "attach-classes": true,

        "indent-switches": true,
        "pad-header": true,
        "align-pointer": "type",
        "unpad-paren": true,

 

utility

fp16库

http://half.sourceforge.net/

https://github.com/Maratyszcza/FP16/tree/master/third-party

 

执行shell命令 system和popen,可以用于执行shell命令,间接获取文件夹下文件名称等

https://www.cnblogs.com/caosiyang/archive/2012/06/25/2560976.html

https://en.cppreference.com/w/cpp/utility/program/system

 

C++ STL正确删除元素方法

	vector data = { 1,2,3,4,5,6,7,8 };

	for (auto itr = data.begin(); itr != data.end();) {
		if (*itr == 8) {
			itr=data.erase(itr);
		}
		else {
			itr++;
		}
	}

data.erase(itr);itr++,或者data.erase(itr++)均不可取!!!处理最后一个元素可能有意外。

 

 

你可能感兴趣的:(C++)