1.通过下标访问
int main()
{
string str("Hello world");
for(string::size_type st=0;st<str.size();++st){
cout<<str[st];
}
return 0;
}
2.通过迭代器访问
int main()
{
string str("Hello world");
for(string::iterator iter=str.begin();iter!=str.end();++iter)
{
cout<<*iter;
}
return 0;
}
3.范围for语句(如果要改变str的值for(auto &c:str)必须是引用而且不能加const)
int main()
{
string str("Hello world");
for(const auto &c:str){ //visual C++ 2010不支持
cout<<c;
}
return 0;
}