#include <iostream> #include <vector> #include <map> #include <string> #include <algorithm > #include <utility> #include <tuple> #include <functional> #include <memory>using namespace Std;void F (int a) {cout<< "int:" <<A<<ENDL;} void F (int *pa) {cout<< "int *:" << pa <<endl;} void Printvector (const vector<int> & VI) {cout<< "["; for (Auto it = Vi.begin (); It! = Vi.end (); ++it) {cout<< *it << ",";} cout<< "]" <<ENDL;} struct Sum{sum () {sum=0;} void operator () (int n) {sum + = n;} int sum;}; Template<typename t>void Print (T t) {cout<< T <<endl; } template<typename Head, TypeName ... tail> void Print (head head, Tail ... Tail) {cout<< head << ","; Print (tail ...); } class Foo{public:foo () {cout<< "Foo construct." << this <<endl;} ~foo () {cout<< "Foo destruct." << this <<endl; }int val;void Bar () {cout<< "Foo.bar () called." << Val <<endl; }};int Main () {/****************************************/auto i = 12345678;auto s = "Hello China"; auto F = 3.14;vector<int > VI {1,2,3,4};d Ecltype (f) d = 6.28; Printvector (vi);cout<< i << endl;cout<< s << endl;cout<< f << endl;//old Style:vecto R<int>::iterator it = Vi.begin () cout<< endl;cout << "Decltype (x):" << D <<endl;/********* Null vs. Nullptrint a = 0;int *pa = Nullptr;int *PB = null; F (a); F (PA); F (PB); bool bequal = (PA = = Pb);cout<< bequal <<endl;/****************************************///for_ eachmap<string,int> M1 {"Hello China", 110},{"USA", 345},{"Japan", 000}};for (auto item:m1) {cout<< "Key:" < <item.first<< ", Value:" <<ITEM.SECOND<<ENDL;} For_each (Vi.begin (), Vi.end (), [] (int & x) {x*=2;}); Printvector (vi); Calls Sum::operator () for each numbersum sum1 = For_each (Vi.begin (), Vi.end (), Sum());cout<< "Sum:" <<sum1.sum<<endl;int Arg1=1,arg2=2;for_each (Vi.begin (), Vi.end (), [=] (int & x ) {x *= (ARG1+ARG2);}); Printvector (vi); For_each (Vi.begin (), Vi.end (), [=] (int & x), int {return x * (ARG1+ARG2);}); Printvector (vi);/****************************************///variable length parameter template auto Pair1 = Make_pair ("Hello China");cout< < "Pair.first:" <<pair1.first<< ", Pair.second:" <<pair1.second<<endl;auto tuple1 = Make_ Tuple ("Hello", 1,3.14);cout<< std::get<0> (tuple1) <<endl;cout<< std::get<1> (tuple1) <<endl;cout<< std::get<2> (tuple1) <<endl; Print (1); Print (1, "Hello China"); Print (1, "Hello China", 3.1415926);cout<< "******************************" <<endl;//unique_ptr with transfer semantics unique _ptr<foo> P1 (New Foo ()), if (p1) P1->val = 123;if (p1) P1->bar (); {unique_ptr<foo> P2 = std::move (p1);p 2->val *=2;p2->bar ();} if (p1) P1->bar ();cout<< "******************************" <≪endl;//shared_ptrshared_ptr<foo> SP1 (new Foo ()), if (SP1) Sp1->val = 123;if (SP1) Sp1->bar (); {shared_ptr<foo> SP2 = Sp1;sp2->val *=2;sp2->bar ();} if (SP1) Sp1->bar (); return 0;}
A new characteristic experiment of c++11