The new usage of the For loop of the "Go" c++11

Source: Internet
Author: User

Objective
C++11 This update brings the for Range loop that many C + + programmers have long awaited, and every time I see JavaScript, the for range in Lua, thinking about how good C + + can be, never mind how sour it is. This time c++11 not to live up to expectations, no longer envy the family for range.

Usage Scenarios
EX1: Traversing strings

1 std::string str = "Hello, World";   2  for (Auto ch:str) {  3      std::cout << ch << Std::endl;   4 }  

Traversing str, outputting each character and using auto at the same time, is a powerful addition.

EX2: Iterating through an array

1 int arr[] = {1234};   2  for (Auto I:arr) {  3      std::cout<< i << std::endl;   4 }  

You can easily iterate through an array without knowing the size of the array container.

EX3: Traversing STL containers

1 std::vector<std::string> Str_vec = {"I", "like",  "google"};   2  for (auto& It:str_vec) {  3      it = "C + +";   4 }  

In this program, reference values can be returned, and the contents of the container can be modified by reference. The initialization list is then used, which is described in the next article.

EX4: Traversing the STL map

 1  std::map<int , Std::string  > hash_map = {{1 , "C + +"}, {2 ," Java "}, {3   2  for   3  std::cout <<  It.first << "\ t" << it.second << Std::endl;  4 } 

The Traverse map returns a pair variable, not an iterator.

Postscript
The for range is simple, but it provides a great convenience for traversing the container, and no longer has to write that long for loop. At the same time , I also confirm that the C + + is complicated, but at the same time it becomes better. It does not matter what features it provides, but if you have been using C + +, or as a qualified programmer, keeping learning is immutable. Why not take a moment to learn about the new features of C + +, not only to improve efficiency, but also to make your code more beautiful, why not?

Original: http://blog.csdn.net/hackmind/article/details/24271949

Reference: https://www.cnblogs.com/jiayayao/p/6138974.html

Https://www.cnblogs.com/pzhfei/archive/2013/03/02/CPP_new_feature.html

http://blog.jobbole.com/44015/

The new usage of the For loop of the "Go" 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.