mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
prepare for c++17
This commit is contained in:
30
code/2/2.4.cpp
Normal file
30
code/2/2.4.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// 2.4.cpp
|
||||
// c++1x tutorial
|
||||
//
|
||||
// created by changkun at shiyanlou.com
|
||||
//
|
||||
// 区间迭代
|
||||
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user