代码如下:
#define _SCL_SECURE_NO_WARNINGS
#include <iostream>
#include <string> //要与c语言里面的#include <string.h>区分
using namespace std;
int main(){
string str("abcdefg");
string::iterator ite;
ite = str.begin();
for (size_t i = 0; i < str.size(); i++){
cout << *ite;
ite++;
}
ite = str.begin();
cout << endl;
for (size_t i = 0; i < str.size(); i++){
cout << ite[i];
}
cout << endl;
ite = str.begin();
for (; ite != str.end(); ite++){
cout << *ite;
}
cout << endl;
str.append(10, 'a');
//cout << ite[16] << endl; 迭代器失效
system("pause");
return 0;
}
运行截图如下: