C++11 基于范围的for循环

【1】基于范围的for循环演化过程

(1)C++98传统写法

 1 // C++98 传统写法
 2 
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int arr[5] = { 1, 2, 3, 4, 5 };
 9 
10     int* p = nullptr;
11 
12     for (p = arr; p < arr + sizeof(arr) / sizeof(arr[0]); ++p)
13     {
14         *p *= 2;
15     }
16 
17     for (p = arr; p < arr + sizeof(arr) / sizeof(arr[0]); ++p)
18     {
19         cout << *p << endl;
20     }
21 }

(2)利用模板库std::for_each写法

 1 // 利用模板库std::for_each写法
 2 
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 void action1(int& e) { e *= 2; }
 8 void action2(int& e) { cout << e << '	';  }
 9 
10 int main()
11 {
12     int arr[5] = { 1, 2, 3, 4, 5 };
13     for_each(arr, arr + sizeof(arr) / sizeof(arr[0]), action1);
14     for_each(arr, arr + sizeof(arr) / sizeof(arr[0]), action2);
15 }

(3)基于范围的for循环

 1 // 基于范围的for循环
 2 
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int arr[5] = { 1, 2, 3, 4, 5 };
 9     for (int& e : arr)
10         e *= 2;
11     for (int& e : arr)
12         cout << e << '	';
13 }

(4)注意(*i)与 e的区别

 1 // 注意(*i)与 e的区别:
 2 
 3 #include <iostream>
 4 #include <vector>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     vector<int> v = { 1, 2, 3, 4, 5 };
10     for (auto i = v.begin(); i != v.end(); ++i)
11         cout << (*i) << endl;  // i是迭代器对象 
12     for (auto e : v)
13         cout << e << endl;     // e是解引用后的对象
14 }

(5)注意使用auto& item : vector

使用引用可少一次拷贝构造过程,区别如下示例:

 1 #include <string>
 2 #include <vector>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 struct student
 7 {
 8     string no;
 9     string name;
10     string age;
11     string grade;
12 
13     student(string no, string name, string age, string grade)
14         : no(no)
15         , name(name)
16         , age(age)
17         , grade(grade)
18     {
19         cout << "contrust" << endl;
20     }
21 
22     student(const student& obj)
23     {
24         cout << "copy contrust" << endl;
25         no = obj.no;
26         name = obj.name;
27         age = obj.age;
28         grade = obj.grade;
29     }
30 };
31 
32 int main()
33 {
34     std::vector<student> vecStu = 
35     {
36         { "100", "liuq", "18", "6"},
37         { "110", "wang", "19", "7"},
38         { "120", "qiang", "20", "8"},
39     };
40 
41     cout << "[1]不带引用的遍历过程:" << endl;
42     for (auto item : vecStu)
43     {
44         cout << item.no << item.name << item.age << item.grade << endl;
45     }
46 
47     cout << "[2]带引用的遍历过程:" << endl;
48     for (auto& item : vecStu)
49     {
50         cout << item.no << item.name << item.age << item.grade << endl;
51     }
52 
53     system("pause");
54 }
55 
56 /* result:
57 contrust
58 contrust
59 contrust
60 copy contrust
61 copy contrust
62 copy contrust
63 [1]不带引用的遍历过程:
64 copy contrust
65 100liuq186
66 copy contrust
67 110wang197
68 copy contrust
69 120qiang208
70 [2]带引用的遍历过程:
71 100liuq186
72 110wang197
73 120qiang208
74 */

good good study, day day up.

顺序 选择 循环 总结

原文地址:https://www.cnblogs.com/Braveliu/p/12245989.html