1. 常规方式
string s="hello"
for(int i=0;i<s.size();i++){
cout<<s[i]<<"";
}
2.迭代器方式
begin接口 : iterator begin();
end 接口: iterator end();
循环方法
string s = "hello";
string::iterator it = s.begin();
while (it != s.end()) {
cout << *it << "";
++it;
}
3. 新for循环(C++11)
1.只读: for(const auto& 变量: 需要遍历的容器)
2.可读可写: for(auto &变量: 需要遍历的容器)
string s = "hello";
for (const auto& ch : s) {
cout << c << "";//只读
}
for (auto& c : s) {
c = 'a';// 可读可写
}