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

C++标准库随着标准的制订,功能越来越丰富,让程序大大减少对第三方库的依赖,更好的支持多种平台 。自己对比较有用的标准库进行了代码测试,供自己查找和大家参考,共同学习进步,废话不多说,直接上代码 。
【C++11/14/17标准库测试代码】#include <stdio.h>#include <string>#include <IOStream>#include <list>#include <vector>#include <map>#include <mutex>#include <shared_mutex>#include <atomic>#include <condition_variable>#include <thread>#include <future>#include <algorithm>#include <unordered_map>#include <regex>#include <locale>#include <codecvt>#include <functional>#if _HAS_CXX17#include <string_view>#endif // _HAS_CXX17#include <memory>#include <array>#include <typeinfo>// autovoid cpp11_auto(){// C++11: auto关键字,自动类型// 32位整数// auto j;auto i = 100;printf("auto i = %dn", i);// 64位整数auto j = 100000000000LL;printf("auto j = %lldLLn", j);// 结构体struct Data { int data = https://www.isolves.com/it/cxkf/yy/C/2020-09-21/0; };auto p = new Data();p->data = 234234;printf("auto p = new Data(), p->data = %d.n", p->data);delete p;// 容器std::map kv;kv["k1"] = 1;kv["k2"] = 2;kv["k3"] = 3;std::map::iterator iter = kv.begin();for(auto it = begin(kv); it != end(kv); it ++){printf("<%s, %d>n", it->first.c_str(), it->second);}}struct S_OBJ{int a;int b;};// loopsvoid cpp11_loops(){// C++11: 区间迭代std::vector sv;sv.push_back(100);sv.push_back(200);sv.push_back(300);for(int &v : sv){printf("v = %dn", v);v ++;}for(auto &v : sv){printf("auto v = %dn", v);}std::map kv = { { "k1", 1 }, { "k2", 2 }, { "k3", 3 } };for(const auto& it : kv){printf("<%s, %d>n", it.first.c_str(), it.second);}std::string str1 = "AbsdfasdfWQErqsadfasdf@!5235safdsaASDFASDF";std::for_each(str1.begin(), str1.end(), [](const char &c){printf("%c", c);});std::transform(str1.begin(), str1.end(), str1.begin(), [](int c) -> int{return std::tolower(c);});std::transform(str1.begin(), str1.end(), str1.begin(), ::toupper);/*static constexpr struct S_OBJ obj ={.a = 2000,.b = 10 * 1000};*/}// enumvoid cpp11_enum(){// C++11: 强类型枚举enum class Options : int64_t { None = 1, One, Two, Three, All };Options o1 = Options::All;Options o2 = Options::Two;}// Lambdavoid cpp11_lambda(){// C++11: Lambda表达式,匿名函数{// 基本Lambda语法auto func = [](int i, const std::string &s){printf("%d, %sn", i, s.c_str());return 0;};func(100, "lambda");}{// 变量捕获 1int i = 10000;auto j = 1.0;auto func1 = [&](const std::string &s){printf("%d, %f, %sn", i, j, s.c_str());};auto func = [&](const std::string &s){printf("%d, %f, %sn", i, j, s.c_str());func1("sdfsadf");};func("lambda");}{// 变量捕获 2class C{public :C() { a = 1111; }void f(int b){auto func = [&](const std::string &s){printf("%d, %d, %sn", a, b, s.c_str());};func("lambda");}private:int a;};C c;c.f(44444);}{// 返回值auto func1 = [](int i) { return i*100; };int ret1 = func1(1);auto func2 = [](int i) -> int { return i*10000; };int ret2 = func2(2);}{// for_eachstd::vector v = { 100, 200, 300 };std::for_each(std::begin(v), std::end(v), [](int n){printf("%dn", n);});auto value = std::find_if(std::begin(v), std::end(v),[](int n) { return n == 100; });if(value != std::end(v)){printf("value = %dn", *value);}else{printf("not foundn");}}/*[]不捕获任何变量[&]以引用方式捕获所有变量[=]用值的方式捕获所有变量(可能被编译器优化为const &)[=, &i]以引用捕获foo, 但其余变量都靠值捕获[bar]以值方式捕获bar; 不捕获其它变量[this]捕获所在类的this指针*/}// mutex & atomicvoid cpp11_mutex_atomic(){//C++11: 互斥量和原子变量{int value = 1000;// 递归锁std::recursive_mutex mutex1;mutex1.lock();value = 20000;mutex1.unlock();{std::lock_guard lock(mutex1);value -= 200;}{std::unique_lock lock(mutex1);value += 200;}}{// 时间递归锁std::recursive_timed_mutex mutex2;{if(mutex2.try_lock_for(std::chrono::milliseconds(200))){printf("lock.n");mutex2.unlock();}}}{// 读写锁std::shared_mutex mutex3;int i = 100;mutex3.lock();i ++;mutex3.unlock();{std::unique_lock locker(mutex3);i ++;}mutex3.lock_shared();int j = i;mutex3.unlock_shared();{std::shared_lock locker(mutex3);j = i;}}/*std::mutex最基本的 Mutex 类 。std::recursive_mutex递归 Mutex 类 。std::time_mutex定时 Mutex 类 。std::recursive_timed_mutex定时递归 Mutex 类 。std::shared_mutex读写锁 Mutex*/// 原子变量std::atomica = 1000;std::atomic b = true;a ++;b = false;}// conditionstatic std::mutexsm;static std::condition_variablesc;void a_thread(){std::unique_lock lock(sm);printf("thread - waiting signal...n");sc.wait(lock);printf("thread - received signal...n");std::this_thread::sleep_for(std::chrono::milliseconds(2000));lock.unlock();printf("thread - notify signal...n");sc.notify_one();}void cpp11_condition(){std::thread at(a_thread);{std::this_thread::sleep_for(std::chrono::milliseconds(1000));}printf("main - notify signal...n");sc.notify_one();{std::unique_lock


推荐阅读