使用C++11的一点总结
一、新特性简介
二、部分常用新特性\库
1、统一初始化
std::vector<std::map<std::string, std::string> > a;
std::map<std::string, std::string> tmp;
tmp.insert(std::make_pair("", ""));
a.push_back(tmp);
std::vector<std::map<std::string, std::string>> a{ { { "", "" } } }
2、函数绑定和lambda
class Work {
public:
void do_work() {
std::cout << "do work" << std::endl;
}
};
int main() {
Work work;
auto bar = std::bind(&Work::do_work, &work);
bar();
}
std::string tmp = "abcdefgdaaa";
tmp.erase(std::remove_if(tmp.begin(), tmp.end(), [](char x) {
return x == 'a';
}), tmp.end());
std::cout << tmp << std::endl;
3、直接给类成员变量设置默认值
class A {
public:
A () {}
explicit A(int new_value) : value(new_value) {}
int get() {
return value;
}
private:
int value = ; // 支持直接写默认值
};
4、新增tuple类型
std::tuple<std::string, std::string, std::string, int, int> record = std::make_tuple("a", "b", "c", , );
std::cout << std::get<>(record) << std::endl;
std::cout << std::get<>(record) << std::endl;
5、新增hash表
6、新增多线程支持
#include <iostream>
#include <functional>
#include <thread>
#include <future>
void show(std::future<int>& fut) {
std::cout << "work thread:" << std::this_thread::get_id() << std::endl;
int x = fut.get(); // 等待获取到结果
std::cout << x << std::endl;
}
int main(){
std::cout << "main thread:" << std::this_thread::get_id() << std::endl;
std::promise<int> promise;
std::future<int> future = promise.get_future();
std::thread work_thread(show, std::ref(future));
std::this_thread::sleep_for(std::chrono::seconds());
promise.set_value();
work_thread.join();
return ;
}
#include <iostream>
#include <string> #define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION #include <boost/thread/future.hpp> using namespace boost; int main() {
future<int> f1 = async([]() { return ; });
future<std::string> f2 = f1.then([](future<int> f) {
std::cout << f.get() << std::endl; // here .get() won't block
return std::string("sgfsdfs");
});
}
7、其它
使用C++11的一点总结的更多相关文章
- C++11用于计算函数对象返回类型的统一方法
[C++11用于计算函数对象返回类型的统一方法] 模板 std::result_of 被TR1 引进且被 C++11 所采纳,可允许我们决定和使用一个仿函数其回返值的类别.底下,CalculusVer ...
- Tips
1)字符串转换数字 ') { ; do{ j*=;j+='); i++; }'); //实际上为读入优化的一部分 2)进制转换万能模板 #include <cstdio> using na ...
- Protocol Buffer技术详解(C++实例)
Protocol Buffer技术详解(C++实例) 这篇Blog仍然是以Google的官方文档为主线,代码实例则完全取自于我们正在开发的一个Demo项目,通过前一段时间的尝试,感觉这种结合的方式比较 ...
- phpMyAdmin安装设置
phpMyAdmin是一种MySQL的管理工具,它直接从web上去管理MySQL. 假设你的web(网页存放)根目录是 /var/www/ 假设你的主机web访问是这样的 http://192.1 ...
- 关于fork( )函数父子进程返回值的问题
fork()是linux的系统调用函数sys_fork()的提供给用户的接口函数,fork()函数会实现对中断int 0x80的调用过程并把调用结果返回给用户程序. fork()的函数定义是在init ...
- emouse思·睿—评论与观点整理之三
虽说我主要做的硬件,平时的兴趣爱好比较关注移动互联网,混迹于虎嗅.爱范儿.雷锋网.36Kr.cnBeta.瘾科技.i黑马.TechWeb等这类科技以及创业媒体,遗憾的是系统的去写的并不多,好在还算充分 ...
- APS审核经验+审核资料汇总——计算机科学与技术专业上海德语审核
1.APS是什么 德国驻华使馆文化处留德人员审核部(简称APS)成立于2001年7月,是由德国驻华使馆文化处和德意志学术交流中心(DAAD)在北京共同合作成立的服务机构. APS是中国学生前往德国留学 ...
- 地区sql
/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...
- Element-UI 2.4.11 版本 使用注意(发现一点更新一点)
1.$Vue.$refs.addForm.resetFields() 的resetFields()方法重置到默认值并不是 ,你在form绑定对象上写的默认值 ,而是这个form被渲染出来之后第一次赋到 ...
随机推荐
- 向STM32 CUBE MX 生成的工程里移植stemwin
我参考这个文章做的: http://bbs.armfly.com/read.php?tid=1678 这次添加的是没有os的版本 另外跟用不用hal库没关系 1. keil自带了emwin 2. 用c ...
- Tomcat崩溃
参考: http://bbs.csdn.net/topics/390391810?page=1 自己遇到的: --------------------------------------------- ...
- 手机响应式js轮播基础
onmousedown --->ontuchstart onmousemove --->ontouchmove onmouseup --->ontouchend ontuchstar ...
- 存储过程里面使用in变量列表异常的处理
在写一个存储过程的时候,由于需要用到类似:select id,name from tablename where id in(id1,id2,id3...)的查询语句,同时括号里面的变量是拼接得到的, ...
- 微信小程序-表单
wxml <view> 按钮: <button size="{{buttom.size}}" type="{{buttom.type}}" p ...
- HMI与设计模式
设计模式是做一个好的架构的一个基础.那么设计模式具体的概念是啥呢?百度百科曰:设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是 ...
- json提交数据到服务端
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Courier New"; color: #393939; backgr ...
- LINQ之路 8: 解释查询(Interpreted Queries)
LINQ提供了两个平行的架构:针对本地对象集合的本地查询(local queries),以及针对远程数据源的解释查询(Interpreted queries). 在讨论LINQ to SQL等具体技术 ...
- CentOS6.5安装Nginx
1.安装prce(重定向支持)和openssl(https支持,如果不需要https可以不安装.) yum -y install pcre* yum -y install openssl* 2.下载n ...
- jQuery 学习笔记
jQuery 学习笔记 一.jQuery概述 宗旨: Write Less, Do More. 基础知识: 1.符号$代替document.getElementById( ...