注意!!本文对比上篇文章《c++中string的用法》进行讲解
一、迭代器的定义
string::iterator it;
二、遍历字符串
string str = "abcdefghijklmn";
//s.begin() 返回字符串s第一个字符的位置
char a = *(str.begin()); // a
//s.end() 返回字符串s最后一个字符串的后一个位置
char b = *(str.end()-1); // n
//s.rbegin() 返回字符串s最后一个字符的位置
char c = *(str.rbegin()); // n
//s.rend() 返回字符串s第一个字符的前一个位置
char d = *(str.rend()-1); // a
for (string::iterator iter = str.begin(); iter != str.end(); iter++)
{
cout << *iter; // 依次输出"abcdefghijklmn"
}
三、插入(insert)
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
//s.insert(s.it,n,ch) 在字符串s的it指向位置上插入n个字符ch
//str.insert(str.begin()+6, 4, 'z'); // str = "hello zzzzworld"
//s.insert(s.it,str) 该函数未定义
//str.insert(str.begin()+6, str2); // error
//s.insert(s.it,str.ita,str.itb) 在字符串s的it指向位置上插入字符串str的[ita,itb)
//str.insert(str.begin()+6, str3.begin()+6, str3.begin()+9); // str = "hello so world"
//s.insert(s.it,cstr,n) 该函数未定义
//str.insert(str.begin()+6, "it is so happy wow", 6); // error
四、替换(replace)
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
//s.replace(s.ita,s.itb,n,ch) 删除字符串s的[ita,itb),然后插入n个字符ch
//str.replace(str.begin(), str.begin()+6, 4, 'z'); // str = "zzzzworld"
//s.replace(s.ita,s.itb,str) 删除字符串s的[ita,itb),然后插入字符串str2
//str.replace(str.begin(), str.begin()+6, str2); // str = "hard world"
//s.replace(s.ita,s.itb,str.itc,str.itd) 删除字符串s的[ita,itb),然后插入字符串str2的[itc,itd)
str.replace(str.begin(), str.begin()+6, str3.begin()+6, str3.begin()+9); // str = "so world"
//s.replace(s.ita,s.itb,cstr,n) 删除字符串s的[ita,itb),然后插入字符数组cstr的前n个字符
//此处不可将"it is so happy wow"替换为str3
//str.replace(str.begin(), str.begin()+6, "it is so happy wow", 6); // str = "it is world"
五、添加(append)
string str = "hello world";
string str2 = "it is so happy wow";
//s.append(str.ita,str.itb) 把字符串str的[ita,itb)添加到当前字符串的结尾
str.append(str2.begin()+6, str2.begin()+9); // str = "hello worldso "
六、赋值(assign)
string str;
string temp = "welcome to my blog";
//s.assign(str.ita,str.itb) 将字符串str的[ita,itb)赋值给字符串s
str.assign(temp.begin()+3, temp.begin()+7); // str = "come"
七、删除(erase)
string str = "welcome to my blog";
//s.erase(s.ita,s.itb) 把当前字符串s的[ita,itb)删除
str.erase(str.begin()+11, str.begin()+14); // str = "welcome to blog"
八、剪切(substr)
string str = "The apple thinks apple is delicious";
//s.substr(s.ita,s.itb) 该函数未定义
string s1 = str.substr(str.begin()+4, str.begin()+9); // error
//s.substr(s.it) 该函数未定义
string s2 = str.substr(str.begin()); // error
九、比较(compare)
string str1 = "small leaf";
string str2 = "big leaf";
//s.compare(s.ita,s.itb,str) 该函数未定义
cout << str1.compare(str1.begin()+2, str1.begin()+9, str2); // error
//s.compare(s.ita,s.itb,str.itc,str.itd) 该函数未定义
cout << str1.compare(str1.begin()+6, str1.begin()+10, str2.begin()+4, str2.begin()+8); // error
//s.compare(s.ita,s.itb,cstr,n) 该函数未定义
cout << str1.compare(str1.begin() + 6, str1.begin() + 10, "big leaf", 4); // error
十、反转(reverse)
string str = "abcdefghijklmn";
reverse(str.begin(),str.end()); // str = "nmlkjihgfedcba"