The use of "c++11" new feature--auto __c++

Source: Internet
Author: User

[+] There are two main uses of auto introduced in c++11: Automatic type inference and the return value placeholder. The semantics of the temporary variable that the auto is identifying in c++98 has been removed in c++11 because it is rarely used and is redundant. Before and after the two standard auto, is completely two concepts.
1. Automatic type inferenceAuto Auto type inference, which is used to infer the data type of a variable from an initialization expression. Auto type inference can greatly simplify our programming work. Here are some examples of using auto.
[CPP]  View plain  copy  print? #include  <vector>   #include  <map>      using namespace  std;      Int main (int argc, char *argv[], char *env[])    {  //  auto a;                  //  error, no initialization expression, cannot infer the type of a (  //  auto)  int a = 10;        //  error, Auto temp variable semantics in C + + ,  is no longer present in 11. This is the use of the old standard.           // 1.  Auto Help derivation type         auto a = 10;       auto c =  ' a ';        auto s ("Hello");          // 2.  type verbose    &NBSP;&NBSP;&NBSP;&NBSP;MAP&LT;INT,&NBSP;MAP&Lt;int,int> > map_;       map<int, map<int,int>>: : Const_iterator itr1 = map_.begin ();       const auto  Itr2 = map_.begin ();       auto ptr = [] ()        {           std::cout <<   "Hello world"  << std::endl;       };           return 0;  };     // 3.  When using template technology, If the type of a variable depends on the template parameter,  //  is difficult to determine the type of the variable without using auto (auto is determined by the compiler).    template <class t, class u>   void multiply (T t,  u u)    {       auto v = t * u;   }  2. The return value occupies a position

[CPP] view plain copy print?   Template <typename T1, typename t2> Auto Compose (T1 T1, T2 T2)-> decltype (t1 + T2) {return t1+t2; Auto V = Compose (2, 3.14); V ' s type is double

3. Use precautions ① we can use Valatile,pointer (*), reference (&), rvalue Reference (&&) to decorate auto
[CPP] view plain copy print?   Auto k = 5; auto* PK = new auto (k);

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.