A new characteristic experiment of c++11

Source: Internet
Author: User

#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:" &LT;&LT;A&LT;&LT;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<< "]" &LT;&LT;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:" &LT;&LT;ITEM.SECOND&LT;&LT;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<< "******************************" &LT;&Lt;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.