C++11/14/17标准库测试代码( 三 )

<< "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


推荐阅读