c++0x新特性实例(比较常用的)
2013-06-17 23:46
260 查看
//array
//auto
//regex
//thread
//future
//enum class
//tuple
//lambda
//final
//override
#include <array> void Foo() { array<int,10> a; generate(a.begin(),a.end(),rand); sort(a.begin(),a.end()); for (auto n:a) { cout<<n<<endl; } cout<<"sizeof(a)="<<sizeof(a)<<endl; }
//auto
#include <vector> void Foo() { auto a = 10; cout<<a<<endl; auto b = 20.0f; cout<<b<<endl; auto& c = a; c++; cout<<a<<endl; vector<int> vec; for(int i = 0; i<10; 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(1)<<endl; cout<<a<<endl; }
//regex
#include <regex> void Foo() { if( regex_match("Hello World!",std::regex("Hello .....!")) ) { cout<<"Math!"<<endl; } if( regex_search("321Hello World!765",std::regex("Hello .....!")) ) { cout<<"Search!"<<endl; } }
//thread
void Foo() { thread t1([] { for (int i = 0; i < 10; i++) { cout<<"t1:"<<i<<endl; } } ); thread t2([] { for (int i = 0; i < 10; 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 Foo() { future<int> f1 = async(Test,1,1); cout<<"f1"<<endl; future<int> f2 = async(Test,2,2); cout<<"f2"<<endl; future<int> f3 = async(Test,3,3); cout<<"f3"<<endl; cout<<f1.get()<<endl<<f2.get()<<endl<<f3.get()<<endl; }
//enum class
void Foo() { #define MAKE_STR(s) #s enum class Type { I = 0, II, III, IV, V }; if (4==(int)Type::V) { cout<<MAKE_STR(Type::V); } }
//tuple
tuple<int,string,float> Do() { return make_tuple(10,"hi",20.0f); } void Foo() { int a = 0; string s = ""; float b = .0f; tie(a,s,b) = Do(); cout<<a<<endl; cout<<s.c_str()<<endl; cout<<b<<endl; }
//lambda
#include <functional> void Foo() { int a = 0; int b = 10; function<int(int)> pA = [&a,b](int i)->int{ return a+=b+i; }; cout<<pA(1)<<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(1); }
//final
class A final { }; /* class B : public A { }; */ class C { virtual void c()final{ } }; class D : public C { //virtual void c(){ } };
//override
class A { virtual void a(){} }; class B : public A { virtual void a()override{} //virtual void a(int i)override{} error //virtual void c()override{} error };
相关文章推荐
- C++ 0X 新特性实例(比较常用的) (转)
- ES6新特性【介绍比较常用的部分】
- php常用字符串比较函数实例汇总
- IOS比较常用的第三方及实例
- 分享ES6中比较常用又强大的新特性
- php中常用的字符串比较函数strcmp()实例解释
- 基础拾遗------特性详解【含常用过滤器实例】
- php常用字符串比较函数实例汇总
- iOS比较常用的第三方及实例(不断更新中)
- 机器学习常用的分类器比较-实例
- Categories IOS比较常用的第三方及实例(不断更新中),ios不断更新
- 有关AutoCompleteBox组件的研究[2]_常用特性实例介绍——Silverlight学习笔记[37]
- IOS比较常用的第三方及实例(不断更新中)
- 基础拾遗------特性详解【含常用过滤器实例】
- php中常用的字符串比较函数strcmp()实例解释
- 比较全面的三层交换机配置实例
- java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】
- 常用的几种进程通信方式的比较(转载)
- Android常用实例—Alert Dialog的使用
- JSP实例数据库常用链接方法