c++0x新特性实例(比较常用的)

//array

 1 #include <array>
 2 
 3 void Foo()
 4 {
 5     array<int,10> a;
 6     generate(a.begin(),a.end(),rand);
 7     sort(a.begin(),a.end());
 8 
 9     for (auto n:a)
10     {
11         cout<<n<<endl;
12     }
13     cout<<"sizeof(a)="<<sizeof(a)<<endl;
14 }

//auto

 1 #include <vector>
 2 void Foo()
 3 {
 4     auto a = 10;
 5     cout<<a<<endl;
 6 
 7     auto b = 20.0f;
 8     cout<<b<<endl;
 9 
10     auto& c = a;
11     c++;
12     cout<<a<<endl;
13 
14     vector<int> vec;
15     
16     for(int i = 0; i<10; i++)
17     {
18         vec.push_back(i);
19     }
20 
21     for(auto i = vec.cbegin(); i!=vec.cend(); i++)
22     {
23         cout<<*i<<endl;
24     }
25 
26     auto pF = [&c](int i)->int{ return c+=i; };
27     cout<<pF(1)<<endl;
28     cout<<a<<endl;
29 }

//regex

 1 #include <regex>
 2 void Foo()
 3 {
 4     if( regex_match("Hello World!",std::regex("Hello .....!")) )
 5     {
 6         cout<<"Math!"<<endl;
 7     }
 8 
 9     if( regex_search("321Hello World!765",std::regex("Hello .....!")) )
10     {
11         cout<<"Search!"<<endl;
12     }
13 
14 }

//thread

 1 void Foo()
 2 {
 3     thread t1([]
 4     {
 5         for (int i = 0; i < 10; i++)
 6         {
 7             cout<<"t1:"<<i<<endl;
 8         }
 9     }
10     );
11 
12     thread t2([]
13     {
14         for (int i = 0; i < 10; i++)
15         {
16             cout<<"t2:"<<i<<endl;
17         }
18     }
19     );
20 
21     t1.join();
22     t2.join();
23 }

//future

 1 #include <future>
 2 int Test(int a,int b)
 3 {
 4     cout<<"Test("<<a<<","<<b<<")"<<endl;
 5     return a+b;
 6 }
 7 
 8 void Foo()
 9 {
10     future<int> f1 = async(Test,1,1);
11     cout<<"f1"<<endl;
12     future<int> f2 = async(Test,2,2);
13     cout<<"f2"<<endl;
14     future<int> f3 = async(Test,3,3);
15     cout<<"f3"<<endl;
16 
17     cout<<f1.get()<<endl<<f2.get()<<endl<<f3.get()<<endl;
18 }

//enum class

 1 void Foo()
 2 {
 3 #define MAKE_STR(s) #s 
 4     enum class Type
 5     {
 6         I = 0,
 7         II,
 8         III,
 9         IV,
10         V
11     };
12 
13     if (4==(int)Type::V)
14     {
15         cout<<MAKE_STR(Type::V);
16     }
17 }

//tuple

 1 tuple<int,string,float> Do()
 2 {
 3     return make_tuple(10,"hi",20.0f);
 4 }
 5 void Foo()
 6 {
 7     int a = 0;
 8     string s = "";
 9     float b = .0f;
10     tie(a,s,b) = Do();
11 
12     cout<<a<<endl;
13     cout<<s.c_str()<<endl;
14     cout<<b<<endl;
15 }

//lambda

 1 #include <functional>
 2 void Foo()
 3 {
 4     int a = 0;
 5     int b = 10;
 6     function<int(int)> pA = [&a,b](int i)->int{ return a+=b+i; };
 7     cout<<pA(1)<<endl;
 8     cout<<a<<endl;
 9 
10     //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
11     cout<<b<<endl;
12 
13     auto pC = [&](int i)->int{ return pA(i); };
14     cout<<pC(1);
15 }

//final

 1 class A final
 2 {
 3 };
 4 /*
 5 class B : public A
 6 {
 7 };
 8 */
 9 class C
10 {
11     virtual void c()final{ }
12 };
13 
14 class D : public C
15 {
16     //virtual void c(){ }
17 };

//override

 1 class A
 2 {
 3     virtual void a(){}
 4 };
 5 
 6 class B : public A
 7 {
 8     virtual void a()override{}
 9     //virtual void a(int i)override{} error
10     //virtual void c()override{} error
11 };
原文地址:https://www.cnblogs.com/mrblue/p/3141456.html