zoukankan      html  css  js  c++  java
  • C++学习-srring类(2)

    一、元素删除原型如下:

    1、iterator erase(iterator first ,iterator last);//删除[first last)字符,返回迭代器指向最后一个被删去的元素。

    2、iterator erase(iterator it);//删除it所指向的字符,返回string中下一个元素的迭代器,没有则返回end()

    3、string& erase(size_type pos=0,size_type n=npos);//删除从pos开始的元素,或删除到末尾,返回删除后的string引用。

    二、元素的插入,函数的重载形式如下(与append有点相像,不局限于尾部,位置参数,插入到该参数的前面)

    1、string& insert(size_type pos,const char *s);

    2、string& insert(size_type pos,const char *s,size_type n);

    3、string& insert(size_type pos,const string& s);

    4、string& insert(size_type pos,const string& s,size_type pos1,size_type n);

    5、string& insert(size_type pos,size_type n char c);

    6、void insert(interator pos,interator first,iterator last);

    7、void insert(iterator pos,size_type n,char c);

     

    三、字符串的比较:

    1、(>,>=,<,<=,==,!=)string(“abcde”)<string(“ac”)

    2、支持compare()来进行字符串的比较

    四、提取字符串:substr成员

    1、str.substr();  //返回str的全部内容

    2、str.substr(5,6);//从第五个开始截取字符一共截取6个字符

    3、str.substr(9);  //截取前九个字符

    五、搜索与查找:

    1、find系列:返回待查找元素或子串在字符串中第一次出现的位置,举例来说,字符串str为“Welcome to C++ World”,使用find函数系列查找单个字符’o’在str中第一次出现位置,返回结果为4,还可查找子串“to”在str中的位置,返回结果为8。

    2、rfind系列:返回待查找元素或子串在字符串中最后一次出现的位置,如使用rfind函数查找单个字符’o’在str中最后一次出现位置,返回结果为16。

    3、find_first_of系列:返回待查找元素在字符串中第一次出现的位置,如果查找的子串,不是查找整个字符串的匹配,而是搜索子串中的字符首次出现的位置,同样以字符串“Welcome to C++ World”为例,使用find_first_of()系列函数查找字符串“abc”在其中首次出现的位置,返回结果为3,此时第一次出现了字符串“abc”中的元素’c’。

    4、find_last_of系列:返回待查找元素在字符串中最后一次出现的位置,如果查找的子串,不是查找整个字符串的匹配,而是搜索子串中的字符最后出现的位置。

    5、find_first_not_of系列:与find_first_of()的工作方式类似,不过搜索的是第一个不位于字符串中的字符第一次出现的位置。

    6、find_last_not_of系列:与find_first_of()的工作方式类似,不过搜索的是第一个不位于字符串中的字符最后一次所在位置。

  • 相关阅读:
    C# 图片与Base64的相互转化
    LeetCode 303. Range Sum Query – Immutable
    LeetCode 300. Longest Increasing Subsequence
    LeetCode 292. Nim Game
    LeetCode 283. Move Zeroes
    LeetCode 279. Perfect Squares
    LeetCode 268. Missing Number
    LeetCode 264. Ugly Number II
    LeetCode 258. Add Digits
    LeetCode 257. Binary Tree Paths
  • 原文地址:https://www.cnblogs.com/lyp1010/p/11690330.html
Copyright © 2011-2022 走看看