int main(){
string s0;
string s1("hello word");
cout<<s1<<endl;
//遍历string,下标+[]
for(size_t i=0;i<s1.size();i++)
{
cout<<s1[i]<<"";
}
cout<<endl;
}
注意:这里size_t不算/0
迭代器
int main()
{
string s0;
string s1("hello world");
cout << s1 << endl;
// 遍历string
cout << s1.size() << endl;
for (size_t i = 0; i < s1.size(); i++)
{
s1[i]++;
}
cout << endl;
s1[0]--;
// 下标+[]
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1[i]<<" ";
}
cout << endl;
char s3[] = "hello world";
s3[1]++; // -> *(s3+1)
s1[1]++; // s1.operator[](1);
// 迭代器
string::iterator it = s1.begin();
while (it != s1.end())
{
// 写
(*it)--;
++it;
}
cout << endl;
it = s1.begin();
while (it != s1.end())
{
// 读
cout << *it << " ";
++it;
}
cout << endl;