<< "str1:" << str1 << std::endl;std::string str2(std::move(str1));std::cout << "str2:" << str2 << std::endl;std::cout << "str1:" << str1 << std::endl;std::vector vec1{ 1, 2, 3, 4, 5 };std::vector vec2(std::move(vec1));std::cout << "vec1:" << vec1.size() << std::endl;std::cout << "vec2:" << vec2.size() << std::endl;std::cout << "vec1:" << vec1.size() << std::endl;// 减少内存拷贝和移动, 向容器中中加入临时对象,临时对象原地构造,没有赋值或移动的操作 。std::vector vec;vec.push_back("this is a text.");vec.emplace_back("this is a text.");}void cpp11_shared_ptr(){// C++11: 智能指针, 允许多个指针指向同一个对象std::shared_ptr s1(new int(100));std::shared_ptr s2(s1);//s1.reset();//s1 = nullptr;auto s3 = std::make_shared(200);bool isEqual = (s1 == s2);printf("s1:%d, s2:%d, s3:%d, s1 == s2:%sn", *s1, *s2, *s3, isEqual ? "y" : "n");class MySharedClass : public std::enable_shared_from_this{public:MySharedClass() { }virtual ~MySharedClass() { }public:void p(){printf("%pn", this);}std::shared_ptr getSharedPtr(){return shared_from_this();}};std::shared_ptr p1(new MySharedClass());std::shared_ptr p2 = p1;printf("use count:%dn", p2.use_count());// 使用std::make_shared可以消除显式的使用 newauto p3 = std::make_shared();p1->p();p2->p();p3->p();p3.reset(); // 计数器减1auto deleteMySharedClass = [](MySharedClass *p){delete p;};// 自定义内存释放函数std::shared_ptr p4(new MySharedClass(), deleteMySharedClass);p4->p();auto p5(std::move(p4));p4->p();p4 = nullptr; // 计数器减1// 独占的智能指针, 引用计数为0或1,不能共享std::unique_ptr up1(new int(500));// std::unique_ptr up2(up1); // errorprintf("up1:%dn", *up1);std::unique_ptr up2(std::move(up1));int* upv = up2.release();delete upv;}void cpp17_string_view(){#if _HAS_CXX17// string_view是C++17引用的类库,避免内存复制std::string_view sv("cpp17 string view");auto s1 = sv.substr(0, 10);const char *str = sv.data();auto str2 = std::string(str, sv.length());for(auto c : s1){}// 调用substr并不会分配新的内存,只是引用原内存地址std::cout << s1 << std::endl;#endif // _HAS_CXX17}void cpp11_others(){{// 参数初始化std::map m{ {"a", 1}, {"b", 2}, {"c", 3} };for(const auto &kv : m){std::cout << kv.first << " : " << kv.second << std::endl;}std::vector iv{5, 4, 3, 2, 1};iv.push_back(6);// array 常量数组std::array ary = { 1, 2, 3, 4 };ary.empty(); // 检查容器是否为空ary.size();// 返回容纳的元素数std::sort(ary.begin(), ary.end(), [](int a, int b){return b < a;});}{// std::this_thread::yield() 当前线程让出自己的CPU时间片(给其他线程使用)// std::this_thread::sleep_for() 休眠一段时间.std::atomic ready(false);while(!ready){// std::this_thread::yield();for(int i = 0; i < 5; i ++){std::this_thread::sleep_for(std::chrono::milliseconds(100));}ready = true;}}{// high resolution clockstd::chrono::time_point ct1 = std::chrono::high_resolution_clock::now();std::this_thread::sleep_for(std::chrono::milliseconds(100));std::chrono::time_point ct2 = std::chrono::high_resolution_clock::now();int64_t systemClock1 = std::chrono::duration_cast(ct1 - std::chrono::time_point()).count();int64_t systemClock2 = std::chrono::duration_cast(ct2 - std::chrono::time_point()).count();int64_t sleepMS = std::chrono::duration_cast(ct2 - ct1).count();int64_t sleepUS = std::chrono::duration_cast(ct2 - ct1).count();std::cout << "sleep milliseconds : " << sleepMS << ", sleep microseconds : " << sleepUS << std::endl;using hrc = std::chrono::high_resolution_clock;std::chrono::time_point ct3 = hrc::now();}{// 元组auto t1 = std::make_tuple(10, "string", 3.14);//std::type_info ti = (int);std::vector veco1;printf("tuple : %d, %s, %.3fn", std::get<0>(t1), std::get<1>(t1), std::get<2>(t1));std::get<1>(t1) = "text";// 对tuple解包int v1 = std::get<0>(t1);std::string v2;double v3;std::tie(v1, v2, v3) = t1;printf("tie(%d, %s, %.3f)n", v1, v2.c_str(), v3);std::tuple t2(3.1415926, "pi");}{// std::unordered_map, std::unordered_setstd::map m1;m1["b"] = 2;m1["c"] = 3;m1["a"] = 1;for(const auto &v : m1){std::cout << v.first << ":" << v.second << std::endl;}std::unordered_map m2;m2["b"] = 2;m2["c"] = 3;m2["a"] = 1;for(const auto &v : m2){std::cout << v.first << ":" << v.second << std::endl;}}{auto x = 1;auto y = 2;// 推导返回类型using type = decltype(x+y);type z; // intz = 100;std::cout << z << std::endl;}{// 原始字符串字面量std::string str = R"(C:\What\The\Fxxksdfsadf345'''23'47523*"(""/'\""")";std::cout << str << std::endl; // C:\What\The\Fxxksdfsadf345'''23'47523*"/'\"""// 支持3种UNICODE编码: UTF-8, UTF-16, 和 UTF-32const charu1[] = u8"I'm a UTF-8 string.u2018";const char16_t u2[] = u"This is a UTF-16 string.u2018";const char32_t u3[] = U"This is a UTF-32 string.U00002018";}{// to string or wstringauto v = std::to_wstring(1000);std::cout
推荐阅读
-
「运势」5月3号运势如虹,财旺运旺,贵人如云,咸鱼翻身发大财的3生肖
-
「运输机」国外媒体称轰20为红色B2,准确消息称今年将首次飞行。军迷们蹲了三天三夜。
-
母亲把高考女儿送错考点当场痛哭|母亲把高考女儿送错考点当场痛哭 究竟怎么回事?
-
-
-
梦泪|梦泪和Fly多排,梦泪拿出马超,Fly拿出花木兰,这真是神仙组合!
-
隔年的橘子皮不就是“陈皮”,为啥能卖到5000一斤?专家说出实情
-
-
央视新闻客户端|阿根廷确诊病例超125万 首都已取消隔离管控
-
-
-
-
王岳伦亲手为李湘画画像,当成品出来的那一刻,这技术没搞错吧?
-
-
生肖▲下月开始,红鸾星动,前缘未了,和前任甜蜜牵手的四大生肖
-
-
-
开幕|“写不尽的世界”李名郎书法作品展,昨日在云栖小镇谷田美术馆开幕
-
梦见梨树是什么意思,好不好,代表什么 梦见梨树是什么意思周公解梦
-