New features of C + + For-each

Source: Internet
Author: User

The C + + Lab class requires a For Each loop to implement the output of the associated container map, beginning with the full-scale. After a long time to find the information to sort out the following:

One of the new features of C++11 is Java-like for Each loop:

1map<int,string>m;2  //13  for(Auto &v:m)4      {5cout<<v.first<<" "<<v.second<<Endl;6     }7 8  //2-LAMDA expression9For_each (M.begin (), M.end (), [] (map<int,string>:: Reference a) {Tencout<<a.first<<" "<<a.second<<Endl; One      }); A  - //3 For_each -  voidFun (map<int,string>::reference a)//don't reference less, or you will get an error.  the { -cout<<a.first<<" "<<a.second<<Endl; - } -For_each (M.begin (), M.end (), fun);

There is also a way to define macros:

1 //definition2 #defineforeach (Container,it)3      for(typeof((container). Begin ()) it = (container). Begin (); it!= (container). end (); + +it)4 //Output5 foreach(m,it)6     {7cout<<it->first<<","<<it->second<<Endl;8}

New features of C + + For-each

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.