string遍历

#include <iostream>
#include <string>

using namespace std;
int main(int argc, const char * argv[]) {

//string str("abcdefg");
string str = "abcdefg";
//string str2 = str;
string str2(str);
//数组形式遍历
for (int i = 0; i < str.length(); i++) {
cout << str[i] << endl;
}

//at方法遍历
for (int i = 0; i < str.length(); i++) {
cout << str.at(i) << endl;
}

//迭代器遍历
for (string::iterator it = str.begin(); it != str.end(); it++) {
cout << *it << endl;
}
return 0;
}

原文地址:https://www.cnblogs.com/xpylovely/p/11193306.html