C++for循环新用法
1、拷贝range的元素时,使用for(auto x : range).
2、修改range的元素时,使用for(auto & x : range).
3、只读range的元素时,使用for(const auto & x : range).
#include <iostream>
#include <vector>
using namespace std;
int main() {
char arc[] = "http://c.biancheng.net/cplus/11/";
//for循环遍历普通数组
for (char ch : arc) {
cout << ch;
}
cout << '!' << endl;
vector<char>myvector(arc, arc + 23);
//for循环遍历 vector 容器
for (auto ch : myvector) {
cout << ch;
}
cout << '!';
return 0;
}
#include <iostream>
using namespace std;
int main() {
for (int num : {1, 2, 3, 4, 5}) {
cout << num << " ";
}
return 0;
}