"Reprint" C++11 's Simple study

Source: Internet
Author: User

First, the c++11 is installed on our development machine and on-line machine , and the catalogue is in:

/opt/compiler/gcc-4.8.2/

Read the following article "C++11" 30 minutes to understand the new features of c++11

Http://www.cnblogs.com/neverdie/p/3767657.html

This person is a game-related industry , and his other articles have time to look again.

C++11 includes a number of new features: a lambda expression, a type deduction keyword auto, a decltype, and a lot of improvements to the template.

New KeywordsAuto
Auto actually compiles the variables at compile time, so it does not adversely affect the running efficiency of the program. In addition, it seems that auto does not affect the compilation speed, because the compile time is also to the right to deduce and then determine whether the left side matches.

Usage:

auto A;//error, Auto is a type deduction through an initialization expression, if there is no initialization expression, you cannot determine the type of a auto i = 1;auto d = 1.0;auto str = "Hello world"; auto ch = ' a '; a Uto func =less <int>(), Vector<int>  Iv;auto ite = Iv.begin (); auto p = new Foo ()//type deduction for custom types

Auto not only has the above application, it also plays a part in the template:

For example, in the case of this processed product,

<Product, typename Creator>void processproduct (const creator& Creator ) { product* val = Creator.makeobject ();
<Creator>void processproduct (const creator& Creator) { Auto val = Creator.makeobject (); Do somthing with Val} discarded the troublesome template parameters, and the entire code became neater.
Decltype

Decltype is actually a bit like auto's inverse function, Auto lets you declare a variable, and decltype can get the type from a variable or an expression.

int x = 3;decltype (x) y = x;

So where is the utility of Decltype, and we go on to continue with the above example:

<Creator>auto processproduct (const creator& Creator), Decltype ( Creator.makeobject ()) {    Auto val = Creator.makeobject ();    Do somthing with Val}

nullptr

Nullptr is a new type introduced to solve the two semantic problem of NULL in the original C + + because NULL actually represents 0

voidFinta) {cout<<a<<Endl;}voidFint*p) {Assert (P!=NULL); cout<< P <<Endl;}intMain () {int*p =nullptr; int*q =NULL; BOOLEqual = (P = = q);//The value of equal is true, indicating that both p and q are null pointers    intA = nullptr;//compilation failed, nullptr cannot be transformed to intF0);//compilation fails in c++98 with two semantics; call F (int) in c++11F (nullptr); return 0;}

sequence for loop

In C + + The For loop can use a simplified for loop like Java,

can be used to iterate over arrays, containers, strings, and sequences defined by the Begin and end functions (that is, there are iterator), the sample code is as follows:

map<stringint> m{{"a"1}, {"b"  2}, {"C"3}};  for (Auto p:m) {    cout<<p.first<<""<<p.second< <Endl;}

lambda expression

Lambda expressions are similar to closures in JavaScript, which can be used to create and define anonymous function objects to simplify the programming effort.

The syntax for Lambda is as follows: [function object parameter] (operator overloaded function parameter)  return value type {function Body}

Vector<int> iv{5,4,3,2,1};intA =2, B =1; For_each (Iv.begin (), Iv.end (), [b] (int&AMP;X) {cout<< (x + b) <<endl;});//(1)For_each (Iv.begin (), Iv.end (), [=](int&x) {x *= (a + b);});//(2)For_each (Iv.begin (), Iv.end (), [=](int&AMP;X)int{returnX * (A + b);});//(3)

The parameters within [] refer to the global variables that a lambda expression can take.
In1In the function, b means that the function can get a global variable outside the lambda expression.
If the = is passed in [], then all external variables can be obtained, such as (2and3) lambda expression
The parameters in () are the arguments passed in each time the function is called.
Plus is the type of the lambda expression return value, such as (3) returns a variable of type int

template for variable length parameters

Tuple:

We are in C + +have used Pair,pair you can use the Make_pair construct to construct a container that contains two different types of data. For example, the following code: Auto P= Make_pair (1,"C + +");
Since the C++The variable-length parameter template was introduced in 11, so a new data type was invented: Tuple,tuple is an n-tuple that can pass in 1, 2 or more different types of data auto T1= Make_tuple (1,2.0,"C + +"); auto T2= Make_tuple (1,2.0,"C + +", {1,0,2}); This avoids the ugly practice of nesting pair in the previous pair, making the code cleaner

Another frequently seen example is the print function:

In C language, printf can pass multiple parameters, in C + +11, we can use variable length parameter template to achieve a more concise printtemplate<typename Head, typename ... tail>  void  Print (head Head, TypeName ... tail) {    cout<< head <<Endl;    Print (Tail ...);} Several different kinds of parameters can be passed in print, as follows: Print (11.0"c++11");

a more elegant initialization method

Before introducing c++11, only arrays can use the initialization list, and other containers want to use the initialization list, only in the following ways:

int arr[3] = {123}vector<int3);

In c++11:

intarr[3]{1,2,3};vector<int> iv{1,2,3};map<int,string>{{1,"a"}, {2,"b"}};stringstr{"Hello World"};

subsequent

For more c++11 reference articles, you can look further at:

Hu Jian's c++11 series blog

Towrting's C++11 Series blog

Compiler support list for c++11

"Reprint" C++11 's Simple study

Related Article

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.