forked from changkun/modern-cpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.4.cpp
More file actions
30 lines (26 loc) · 620 Bytes
/
2.4.cpp
File metadata and controls
30 lines (26 loc) · 620 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//
// 2.4.cpp
// c++1x tutorial
//
// created by changkun at changkun.de
//
// 区间迭代
#include <iostream>
#include <vector>
int main() {
int array[] = {1,2,3,4,5};
for(auto &x : array) {
std::cout << x << std::endl;
}
// 传统 C++ 写法
std::vector<int> arr(5, 100);
for(std::vector<int>::iterator i = arr.begin(); i != arr.end(); ++i) {
std::cout << *i << std::endl;
}
// C++11 写法
// & 启用了引用, 如果没有则对 arr 中的元素只能读取不能修改
for(auto &i : arr) {
std::cout << i << std::endl;
}
return 0;
}