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

lock(sm);sc.wait(lock);printf("main - received signal...n");}at.join();printf("main - thread exit...n");}// threadvoid thread_proc(int a, int b){printf("a = %dn", a);}void cpp11_thread(){// C++11: std::thread 线程std::thread aThread = std::thread([]() -> int{std::this_thread::sleep_for(std::chrono::milliseconds(100));return 1;});if(aThread.joinable()){aThread.join();}std::thread threads[5];for(int i = 0; i < 5; i ++){threads[i] = std::thread(thread_proc, i, 100);}for(auto& t : threads){t.join();}}void cpp11_future(){// C++11: std::future 可以用来获取异步任务的结果// std::launch::async: 直接创建线程// std::launch::deferred: 延迟创建线程(当遇到future.get或者future.wait的时候才会创建线程)// 默认为std::launch::async | std::launch::deferred// 这个就不可控了,由操作系统根据当时的运行环境来确定是当前创建线程还是延迟创建线程 。std::future f = std::async(std::launch::async, [](int a){std::this_thread::sleep_for(std::chrono::seconds(2));return a;}, 100);// 阻塞一直等待结果返回int a = f.get();printf("a = %dn", a);}void cpp11_class(){class Base{public:// Base() = default; // 缺省构造Base(){value1 = 0;value2 = 2;}// 委托构造函数Base(int value) : Base(){value2 = value;}virtual int foo1(){return value2;}virtual int foo2(int a) final{return a;}private:int value1 = 0;int value2 = 2;};Base base(3);class Sub1Class final : Base{public:using Base::Base; // 继承构造int foo1() override{return 2;}// virtual int foo2(int a) override { return a * 2; } // 非法, Base的foo2函数已是final// virtual int foo5(float) override { return 0; }// 非法, 父类没有此虚函数};// class Sub2Class : Sub1Class {}; // 非法, Sub1Class 已 finalSub1Class s1(4);}void cpp11_regex(){std::string fnames[] = { "foo.txt", "bar.txt", "bar.exe", "test", "a0bc.txt", "AAA.txt" };{std::regex base_match("[a-z]+/.txt");for(const auto &fname : fnames){bool ret = std::regex_match(fname, base_match);printf("%s:%sn", fname.c_str(), ret ? "true" : "false");}}{std::regex base_regex("([a-z]+)\.([a-z]+)");for(const auto &fname : fnames){std::smatch base_match;bool ret = std::regex_match(fname, base_match, base_regex);if(ret){// sub_match 的第一个元素匹配整个字符串// sub_match 的第二个元素匹配了第一个括号表达式if(base_match.size() == 3){printf("sub-match[0] : %s, sub-match[1] : %s, sub-match[2] : %sn",base_match[0].str().c_str(), base_match[1].str().c_str(), base_match[2].str().c_str());}}}}}const std::string wstring2utf8(const std::wstring& src){std::wstring_convert> conv;return conv.to_bytes(src);}const std::wstring utf8_2_wstring(const std::string& src){std::wstring_convert > conv;return conv.from_bytes(src);}void cpp11_wstring_utf8(){// codecvt_utf8: <=> utf-8// codecvt_utf8_utf16: utf-16 <=> utf-8// codecvt_utf16: <=> std::stringstrUTF8= wstring2utf8(L"测试codecvt代码");std::wstring wstrUCS1 = utf8_2_wstring(strUTF8);std::wstring wstrUCS2 = utf8_2_wstring(u8"这是UTF-8字符串");wprintf(L"UCS1:%snUCS2:%sn", wstrUCS1.c_str(), wstrUCS2.c_str());return ;}int func1(int, const char*){return 0;}void cpp11_functional(){// C++11: std::function的实例可以对任何可以调用的目标实体进行存储、复制、和调用操作// 这些目标实体包括普通函数、Lambda表达式、函数指针、以及其它函数对象等auto callback = [](std::function func){return func(100, "string");};std::function func;func = func1;func(1, "sdfsdf");callback([](int v, const char *str){printf("%d,%sn", v, str);return 0;});class MyBase{public:virtual int print2(int v, const char *str) {return 0; }};class MyFunc:public MyBase{public:static int print(int v, const char *str){printf("MyFunc::print(%d, %s)n", v, str);return 0;}int print2(int v, const char *str){MyBase::print2(v, str);printf("MyFunc::print2(%d, %s)n", v, str);return 0;}};callback(&MyFunc::print);MyBase *mb1 = new MyFunc();MyFunc my1;mb1->print2(1, "df");// 使用std::placeholders::_1, std::placeholders::_2对参数进行占位callback(std::bind(&MyBase::print2, &my1, std::placeholders::_1, std::placeholders::_2));(&my1)->MyBase::print2(100, "a string");std::function func2 =std::bind(&MyFunc::print2, &my1, std::placeholders::_1,std::placeholders::_2);func2(1000, "a string");// std::ref, std::cref, 在模板传参的时候传入引用,否则无法传递auto test_function = [](std::function fun){fun(1);};auto foo1 = [](int& a) -> void{++ a;};auto foo2 = [](const int& a){std::cout << "a=" << a << "n";};int a = 1;// 变量a被复制,并没有对a引用auto f1 = std::bind(foo1, a);test_function(f1);std::cout << "a=" << a << "n";// std::ref 是一个函数,返回 std::reference_wrApper(类似于指针)test_function(std::bind(foo1, std::ref(a)));std::cout << "a=" << a << "n";test_function(std::bind(foo2, std::cref(a)));std::cout << "a=" << a << "n";}void cpp11_move(){// C++11: move construct, 移动构造(&&),减少内存分配和复制std::string str1 = "this is a string.";std::cout


推荐阅读