C++STL容器---string常用方法介绍

1.1 string基本概念

  • string是C++风格的字符串,而string本质上是一个类
  • char * 是一个指针
  • string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。

1.2 string构造函数

函数签名:

  1. string(); :创建一个空的字符串

     string str;
    
  2. string(const char* s);: 使用字符串s初始化

        const char *s="hello";
        string str(s);
    
  3. string(const string& str);: 使用一个string对象初始化另一个string对象

    	string s="hello";
        string str(s);
    
  4. string(int n, char c); : 使用n个字符c初始化

     	string str(3,'h');
        cout<<str<<endl;//hhh
    

1.3 string赋值操作

函数签名:

  1. string& operator=(const char* s);: char*类型字符串 赋值给当前的字符串

     	const char * p="hello";
        string str=p;
    //或者
    	string str="hello"; //"hello"本身就是const char *类型
    
  2. string& operator=(const string &s); : 把字符串s赋给当前的字符串

     	string s="hello";
        string str=s;
    
  3. string& operator=(char c);: 字符赋值给当前的字符串

    	string str;
        str='a';// 赋值可以写str='a' 但是不能在初始化的时候直接string str='a';	
    
  4. string& assign(const char *s);: 把字符串s赋给当前的字符串

     	string str;
        str.assign("hello");
    
  5. string& assign(const char *s, int n);: 把字符串s的前n个字符赋给当前的字符串

    	string str;
        str.assign("hello",3);
        cout<<str<<endl;//hel
    
  6. string& assign(const string &s); : 把字符串s赋给当前字符串

     	string s="hello";
        string str;
        str.assign(s);
        cout<<str<<endl;//hello
    
  7. string& assign(int n, char c); : 用n个字符c赋给当前字符串

     	string str;
        str.assign(5,'h');
        cout<<str<<endl;//hhhhh
    

1.4 string字符串拼接

  1. string& operator+=(const char* str); : 重载+=操作符

     	string str="hello";
        str+=" world";
    
  2. string& operator+=(const char c);: 重载+=操作符

    	string str="hello";
        str+='H';//也可以str+=97 但是实际上拼接的是将数字对应的ascii的字符
    
  3. string& operator+=(const string& str);: 重载+=操作符

      	string str="hello";
        string s=" world";
        str+=s;
    
  4. string& append(const char *s); : 把字符串s连接到当前字符串结尾

      	string str="hello";
        str.append(" world");
    
  5. string& append(const char *s, int n); : 把字符串s的前n个字符连接到当前字符串

     	string str="hello";
        str.append("world",3);
        cout<<str<<endl;//hellowor
    
  6. string& append(const string &s); : 同operator+=(const string& str)

     	string str="hello";
        string s="world";
        str.append(s);
        cout<<str<<endl;//helloworld
    
  7. string& append(const string &s, int pos, int n);: 字符串s中从pos开始的n个字符连接到字符串结尾

    	string str="hello";
        string s="world";
        str.append(s,1,3);
        cout<<str<<endl;//helloorl
    

1.5 string查找和替换

  1. int find(const string& str, int pos = 0) const;: 查找str第一次出现位置,从pos开始查找

     	string str="helloworldhello";
        string sub="he";
        int pos=str.find(sub,0);
        cout<<pos<<endl;//0
        pos=str.find(sub,3);
        cout<<pos<<endl;//10
    
  2. int find(const char* s, int pos = 0) const; : 查找s第一次出现位置,从pos开始查找

      	string str="helloworldhello";
        int pos=str.find("he",0);
        cout<<pos<<endl;//0
        pos=str.find("he",3);
        cout<<pos<<endl;//10
    
  3. int find(const char* s, int pos, int n) const; : 从pos位置查找s的前n个字符第一次位置

    	string str="helloworldhello";
        int pos=str.find("hello",0,2);//查找"he"
        cout<<pos<<endl;//0
        pos=str.find("hello",3,2);
        cout<<pos<<endl;//10
    
  4. int find(const char c, int pos = 0) const; : 查找字符c第一次出现位置

    	 string str="helloworldhello";
        int pos=str.find('h',0);
        cout<<pos<<endl;//0
        pos=str.find('h',3);
        cout<<pos<<endl;//10
    
  5. int rfind(const string& str, int pos = npos) const; : 查找str最后一次位置,从pos开始查找

        string str="helloworldhello";
        string sub="he";
        int pos=str.rfind(sub,5);//5-->0寻找
        cout<<pos<<endl;//0
        pos=str.rfind(sub,12);//12-->0寻找
        cout<<pos<<endl;//10
    
  6. int rfind(const char* s, int pos = npos) const;: 查找s最后一次出现位置,从pos开始查找

     	string str="helloworldhello";
        int pos=str.rfind("he",5);//5-->0寻找
        cout<<pos<<endl;//0
        pos=str.rfind("he",12);//12-->0寻找
        cout<<pos<<endl;//10
    
  7. int rfind(const char* s, int pos, int n) const; : 从pos查找s的前n个字符最后一次位置

    	 string str="helloworldhello";
        int pos=str.rfind("hello",5,3);//5-->0寻找"hel"
        cout<<pos<<endl;//0
        pos=str.rfind("hello",12,4);//12-->0寻找"hell"
        cout<<pos<<endl;//10
    
  8. int rfind(const char c, int pos = 0) const; : 查找字符c最后一次出现位置

     	string str="helloworldhello";
        int pos=str.rfind('h',5);//5-->0
        cout<<pos<<endl;//0
        pos=str.rfind('h',12);//12-->0
        cout<<pos<<endl;//10
    
  9. string& replace(int pos, int n, const string& str); : 替换从pos开始n个字符为字符串str

    	string str="helloworldhello";
        string s="HELLO";
        str.replace(0,5,s);
        cout<<str<<endl;//HELLOworldhello
    
  10. string& replace(int pos, int n,const char* s); : 替换从pos开始的n个字符为字符串s

     	 string str="helloworldhello";
        str.replace(0,5,"HELLO");
        cout<<str<<endl;//HELLOworldhello
    

1.6 string字符串比较

  • 字符串比较是按字符的ASCII码进行对比

    = 返回 0

    > 返回 1

    < 返回 -1

  1. int compare(const string &s) const; : 与字符串s比较

     	string s1="hello";
        string s2="hello";
        int cmp=s1.compare(s2);
        cout<<cmp<<endl;//0
    
  2. int compare(const char *s) const; : 与字符串s比较

     	string s1="hello";
        int cmp=s1.compare("hello");
        cout<<cmp<<endl;
    

判断两个字符串是否相等经常使用的是==

	 string s1="hello";
    int cmp=s1=="hello";
    cout<<cmp<<endl;//1

1.7 string字符存取

  1. char& operator[](int n); : 通过[]方式取字符

    string s="hello";
    cout<<s[0]<<end;
    s[0]='H'; //因为返回的是一个引用 所以可以作为左值进行赋值操作
    
  2. char& at(int n); : 通过at方法获取字符

    	string s="hello";
        cout<<s.at(0)<<endl;
        s.at(0)='H';
        cout<<s.at(0)<<endl;
    

1.8 string插入和删除

  1. string& insert(int pos, const char* s); : 插入字符串

     	string s="hello";
        s.insert(1,"OOOO");
        cout<<s<<endl;//hOOOOello
    
  2. string& insert(int pos, const string& str); : 插入字符串

    	string s="hello";
    	string s1="OOOO";
        s.insert(1,s1);
        cout<<s<<endl;//hOOOOello
    
  3. string& insert(int pos, int n, char c); : 在指定位置插入n个字符c

    	string s="hello";
        s.insert(1,4,'O');
        cout<<s<<endl;//hOOOOello
    
  4. string& erase(int pos, int n = npos); : 删除从Pos开始的n个字符

     	string s="hOOOOello";
        s.erase(1,4);
        cout<<s<<endl;//hello
    

1.9 string子串

  • string substr(int pos = 0, int n = npos) const; : 返回由pos开始的n个字符组成的字符串

       	string s="hello world";
        string subStr=s.substr(1,3);
        cout<<subStr<<endl;//ell
    

猜你喜欢

转载自blog.csdn.net/qq_43478694/article/details/127471784
今日推荐