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
推荐阅读
-
生二胎到底是值不值?过来人:用3年辛劳换30年幸福,真的很值
-
-
-
-
「言车社」30万的特斯拉Model3没质感?这台车完胜
-
勒布朗·詹姆斯|狂轰28+12+8,詹皇刷爆5大神迹,凶狠犯规+争议吹罚挡不住他
-
-
艾滋病:当艾滋病恐惧症极端情绪来临时 我们应该如何应对和脱恐
-
-
[塞尔维亚]塞尔维亚:我们不需要钱,中国网友:不,你需要,这大概就是友谊
-
-
祥说篮球|东部五虎或被4-0横扫?最贵阵容变败笔,2.7亿双巨头合砍17分
-
【黄璐琦】中国工程院院士黄璐琦:目前湖北地区病例中医药参与
-
白酒:普通香型白酒最佳饮用期是5年,酱香酒是15年!你买的什么酒?
-
-
美丽一点 奈何自己都还走不稳,非常暖心,暖哭的一幕:弟弟想牵着姐姐
-
-
阿力讲星座|3属相遇流年旺财,生意红火,财气连绵,婚姻美满!,8.7日开始
-
央视新闻|两部门:鼓励基层群众性自治组织参与城乡社区防汛救灾工作
-
周到|旅美熊猫家族添新丁,四年之后它也会坐“熊猫快递号”专机回家吗?