Learn the new grammar in c++11--c++11

Source: Internet
Author: User

c++11 new Syntax 1:auto keyword
One of the most useful features that c++11 adds is the Auto keyword.
I wonder if you've ever written such a code:

STD::MAP<STD::string, std::vector<std::shared_ptr<std::list<t> > > > map;std:: Map<std::string, std::vector<std::shared_ptr<std::list<t> > > >::iterator it = Map.begin ();


Even more complex templates are nested than this.
In this case, not only is the code lengthy and error prone, but the compilation error message after the error is difficult to read
The use of auto can greatly simplify the writing of the code, but also reduce the possibility of spelling mistakes

STD::MAP<STD::string, std::vector<std::shared_ptr<std::list<t> > > >= Map.begin ();


C++11 New Grammar 2:nullptr
Before nullptr, the null pointer is defined as follows:

#ifdef __cplusplus    #define Null    0   #else   #define null    ((void *) 0)   #endif   

Because C + + is strongly typed, void * is not implicitly convertible to other pointer types, so the null of C + + is directly defined as 0.
There's a big mistake in this case.

void foo (int  i);   void foo (char* p);

In this case:

Foo (NULL);

is not clear.
So the nullptr was introduced into the c++11.

Foo (0); foo (nullptr);

Solved the problem.


C++11 New Grammar 2:for loop
The general for loop in C + + is as follows:

std::vector<int> v;  for (Auto it = V.begin (); It! = V.end (); it++)    {//doanything}

Other languages, such as the For loop in Python, are like this

 for inch list:     # Do anything

Will cpper feel blue?
So c++11 introduced this:

 for (Auto v:vector) {    // do anything}


These are some of the most common new grammars in c++11.

Abelkhan Technology Forum: http://abelkhan.com/forum.php, Welcome to Exchange technology

Learn the new grammar in c++11--c++11

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.