C++ string 中的find函数与 erase 函数

 find函数的变形较多。感觉会用   .find()    寻找单个字符首次出现的位置,或者字符串首次出现的位置就够了。

    string::size_type index;
    string a = "abcdef";
    string b = "def";
    index = a.find(b);
    if(index != string::npos)  ///则代表在 a 中找到了 b, index是b在a中首次出现的首个字母的下标
                               ///如此处 index 的值为  3
    if(index == string::npos)  ///表示未找到

erase函数的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是说有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个 字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符 (first和last都是迭代器)

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/81407200