Foreach of the principle of variable parameters and characteristics using c ++ 11

Wrote c ++ 11 feature uses , but this what is it, and foreach statement is very similar to other languages

Function must have known at compile time single return type; when the compiler can figure out from context what it must be when, autojust so you do not enter it. So you give a template class, if it does not compile time type, must not pass, we used the map is in fact calling code this library, of course, map itself is not compiled and run, it must be KeyValue type parameters.

Of course, auto C ++ 14 and 11 are not the same, in that article only describes the use of auto, we are unable to see the difference between, we realize a function of all the elements of the vector and returns * 3

//C++98
std::vector<int> &multiply_by_three(std::vector<int> &v)
{
    for (std::vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        *it *= 3;
    }
    return v;
}

Beginners very easy to forget how to spell iterator, sometimes wrong

// C ++. 11 
STD :: Vector < int > & multiply_by_three (STD :: Vector < int > & V) 
{ 
    // this is a reference to & rather than seeking address operator, can be directly operated for speed
     // (reference pointers and will not create a new object and the cost of constructing the object destructor object may be bulky.) 
    for (Auto & IT: V) 
    { 
        IT * = . 3 ; 
    } 
    return V; 
}

Faster, better writing

auto& multiply_by_three(std::vector<int>& v) 
{
      for(auto& it : v) 
      {
          it *= 3;
      }
      return v;
}

Support the return type of value judgment, we sometimes write anonymous functions, such as sort this

 sort(S.begin(),S.end(),[]
    (const string &s,const string &c)
    {
      return s>c;
    }
    );

In fact, these are all syntactic sugar Lambda

The following are various variables intercepted options:
  • [] Not taken any variable
  • [&} External scope all variables taken, and used as a reference in the body of the function
  • [=] Taken outside scope all variables, and a copy function in the body
  • [=, & Foo] taken outside scope all variables used in the copy function and the body, but the use of a reference variable foo
  • [Bar] bar taken and variables used in the copy function of body weight, while other variables not taken
  • [This] taken this pointer in the current class. If you have used default & or = to add this option.

Syntactic sugar is a more or less almost every language has provided some of the programmers to develop code syntax, it's just compiler implements some tricks fills during compilation of these in a specific byte code or a specific way the syntax to do some processing, developers can easily use directly. This syntax is no effect on the function of language itself, it is only for the convenience of programmers to develop and improve development efficiency. To put it plainly, syntactic sugar is a package of existing grammars. These syntactic sugar, although does not offer substantial improvements, or they can improve performance, or can improve the rigor of grammar, or can reduce the chance of coding errors.

We can see him traverse assembly code what is the difference

Use the -S command, such as gcc -g -S B.cpp

I did not compare what ah, some instructions are not the same, but I do not see too understand

Guess you like

Origin www.cnblogs.com/BobHuang/p/11220700.html