C++ 0X 新特性实例(比较常用的) (转)
转自:http://www.cnblogs.com/mrblue/p/3141456.html
//array
#include <array>
void Foo1()
{
array<int, > a;
generate(a.begin(), a.end(), rand);
sort(a.begin(), a.end()); for (auto n:a)
{
cout<<n<<" ";
}
cout<<"sizeof(a) = "<<sizeof(a)<<endl;
}
//regex
#include <regex>
void Foo2()
{
if( regex_match("Hello World!", regex("Hello World!")) )
{
cout<<"Math!"<<endl;
} if (regex_search("321Hello World!765", std::regex("Hello")) )
{
cout<<"Search!"<<endl;
}
}
//thread
#include <thread>
void Foo3()
{
thread t1([]
{
for (int i = ; i < ; i++)
{
cout<<"t1:"<<i<<endl;
}
}
); thread t2([]
{
for (int i = ; i < ; i++)
{
cout<<"t2:"<<i<<endl;
}
}
); t1.join();
t2.join();
}
//future (暂时没理解)
#include <future>
int Test(int a, int b)
{
cout<<"Test("<<a<<","<<b<<")"<<endl;
return a+b;
}
void Foo4()
{
future<int> f1 = async(Test,,);
cout<<"f1"<<endl; future<int> f2 = async(Test,,);
cout<<"f2"<<endl; future<int> f3 = async(Test,,);
cout<<"f3"<<endl; cout<<f1.get()<<endl<<f2.get()<<endl<<f3.get()<<endl;
}
//enum class
void Foo5()
{
#define MAKE_STR(s) #s
enum class Type
{
I = ,
II,
III,
IV,
V
}; if( ==(int)Type::V )
cout<<MAKE_STR(Type::V)<<endl;
}
//auto
#include <vector>
void Foo6()
{
auto a = ;
cout<<a<<endl; auto b = 20.0f;
cout<<b<<endl; auto& c = a;
c++;
cout<<a<<endl; vector<int> vec; for(int i = ; i<; i++)
{
vec.push_back(i);
} for(auto i = vec.cbegin(); i!=vec.cend(); i++)
{
cout<<*i<<endl;
} auto pF = [&c](int i)->int{ return c+=i; };
cout<<pF()<<endl;
cout<<a<<endl;
}
//lambda
#include <functional>
void Foo7()
{
int a = ;
int b = ;
function<int(int)> pA = [&a,b](int i)->int{ return a+=b+i; };
cout<<pA(a)<<endl;
cout<<a<<endl; //function<int(int)> pB = [&a,b](int i)->int{ return b+=a+i; };
//compile error : 'b': a by-value capture cannot be modified in a non-mutable lambda
cout<<b<<endl; auto pC = [&](int i)->int{ return pA(i); };
cout<<pC()<<endl;
}
C++ 0X 新特性实例(比较常用的) (转)的更多相关文章
- c++0x新特性实例(比较常用的)
//array #include <array> void Foo() { array<> a; generate(a.begin(),a.end(),rand); sort( ...
- 【学亮IT手记】Java 8新特性实例介绍
java8,也称为jdk1.8,于2014.03.18日发布,它支持函数式编程,新的js引擎,新的日期API,新的Stream Api等. 我们主要讨论以下几个新特性: ①Lambda表达式. 允许把 ...
- C++0x新特性
我是在一个帖子上摘抄的大神语录...感谢supermegaboy大神,给了详尽的解释 下文是一篇转载的Wikipedia的译文,从语言和库双方面概述了C++0x. 右值引用与转移语义 在标准C++语言 ...
- es6/es7/es8常用新特性总结(超实用)
本文标题有误导性,因为我其实想写node8的新特性,说实话一下子从node v1.x跳跃到node 8.x+ 真有点受宠若惊的感觉.一直觉得node 数组. 对象.序列等的处理没有python方便,因 ...
- JDK8新特性(二) 流式编程Stream
流式编程是1.8中的新特性,基于常用的四种函数式接口以及Lambda表达式对集合类数据进行类似流水线一般的操作 流式编程分为大概三个步骤:获取流 → 操作流 → 返回操作结果 流的获取方式 这里先了解 ...
- 分享ES6中比较常用又强大的新特性
前言 es6有很多新东西,但是感觉常用的并不是很多,这里学习记录了一些我自己认为非常常用又强大的新特性. scoping 实用的块级作用域,let x = xxx 可以声明一个块级作用域的局部变量,简 ...
- ES6常用新特性
https://segmentfault.com/a/1190000011976770?share_user=1030000010776722 该文章为转载文章!仅个人喜好收藏文章! 1.前言 前几天 ...
- java-API中的常用类,新特性之-泛型,高级For循环,可变参数
API中的常用类 System类System类包含一些有用的类字段和方法.它不能被实例化.属性和方法都是静态的. out,标准输出,默认打印在控制台上.通过和PrintStream打印流中的方法组合构 ...
- 常用的HTML5、CSS3新特性能力检测写法
伴随着今年10月底HTML5标准版的发布,未来使用H5的场景会越来越多,这是令web开发者欢欣鼓舞的事情.然而有一个现实我们不得不看清,那就是IE系列浏览器还占有一大部分市场份额,以IE8.9为主,w ...
随机推荐
- Android 一个强大的图片选择器
看看下面的图片选择器,感觉很不错 http://www.jcodecraeer.com/a/anzhuokaifa/2017/0122/7083.html 看看下面的图片选择器,支持视频等 http: ...
- go语言之接口二
接口查询: 先来看如下的结构.结构体File实现了Read,Writer,Seek,Close的方法 type File struct{ } func (f *File) Read(buf []byt ...
- require.js vs browserify
require.js vs browserify require.js是模块加载器:browserify是预编译工具 require.js遵循的是AMD规范:browserify遵循的是CommonJ ...
- SAP号码段
[转http://blog.csdn.net/wengyupeng/article/details/8513527] 1.通用Tcode:SNRO 常用维护特定Number range Tcode: ...
- Struts详解
1.什么是MVC? MVC是Model,View,Controller的缩写,MVC是Application开发的设计模式, 也就是大家所知道的Model2.在MVC的设计模式中,它包括三类对象:(1 ...
- swap 内存不足
参考:https://stackoverflow.com/questions/5682854/why-is-the-linker-terminating-on-me-when-i-build-clan ...
- Ionic常见问题
1.sh: 1: glxinfo: not found sudo apt-get update && sudo apt-get install mesa-utils 2.ionic s ...
- [原创]Scala学习:编写Scala脚本
scala支持脚本 1)在/opt/scala-script下创建一个文件hello.scala 编辑内容如下: $ hello ,this is the first scala script 2)运 ...
- P2163 [SHOI2007]园丁的烦恼
题目 P2163 [SHOI2007]园丁的烦恼 做法 关于拆点,要真想拆直接全部用树状数组水过不就好了 做这题我们练一下\(cdq\)分治 左下角\((x1,y1)\)右上角\((x2,y2)\), ...
- 第七篇、os、sys、random、time、datetime、logging
一.sys 用于提供对Python解释器相关的操作: 1 2 3 4 5 6 7 8 9 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) ...