zoukankan      html  css  js  c++  java
  • string类的用法总结

    string中常见的成员函数


    示例代码:

    string s= string("abcdefg");
    char ch[] = "abcdefgd";
    
    //调用构造函数全部复制
    string str1 = string(s);
    
    //构造函数,从字符串str的第2个元素开始,复制5个元素,赋值给str2
    string str2 = string(s,2,5);
    
    //复制ch前5个字符串
    string str3 = string(ch,5);
    
    //将5个'6'赋给字符串
    string str4 = string(5,'6');
    
    //
    string str5 = string(s.begin(),s.end());
    
    cout << "oringin s = " + s << endl;
    cout << "oringin ch = " <<ch << endl;
    cout << "str1 = " + str1<< endl;
    cout << "str2 = " + str2<<endl;
    cout << "str3 = " + str3<<endl;
    cout << "str4 = " + str4<<endl;
    cout << "str5 = " + str5<<endl;
    

    输出结果:

    string的常用方法

    取string中元素

    使用at()或[]去字符串的元素

    string s = "hello";
    char c = s.at(2);
    char ch = s[2];
    

    上面两行的效果是一样的,都是将l取出。

    字符串比较

    1. compare函数
      函数原型如下所示:

      int compare (const basic_string& s) const;
      int compare (const Ch* p) const;
      int compare (size_type pos, size_type n, const basic_string& s) const;
      int compare (size_type pos, size_type n, const basic_string& s,size_type pos2, size_type n2) const;
      int compare (size_type pos, size_type n, const Ch* p, size_type = npos) const;

    函数返回值介绍:若参与比较的两个串值相同,则函数返回 0;若字符串 S 按字典顺序要先于 S2,则返回负值;反之,则返回正值。下面举例说明如何使用 string 类的 compare() 函数。

    具体示例如下:

    string s1 = "Hello";
    string s2 = "Ok";
    string s3 = "abdcds";
    string s4("abdcds");
    
    cout << "s1 = " + s1 <<endl;
    cout << "s2 = " + s2 <<endl;
    cout << "s3 = " + s3 <<endl;
    cout << "s4 = " + s4 <<endl;
    
    cout<<"s1.comapre(s2) = " << s1.compare(s2) <<endl;
    cout<<"s2.comapre(s3) = " << s2.compare(s3) <<endl;
    cout<<"s3.comapre(s4) = " << s3.compare(s4) <<endl;
    

    输出结果如下:

    s1 = Hello
    s2 = Ok
    s3 = abdcds
    s4 = abdcds
    s1.comapre(s2) = -1
    s2.comapre(s3) = -1
    s3.comapre(s4) = 0
    
    1. 比较运算符
      String 类的常见运算符包括 >、<、==、>=、<=、!=。其意义分别为"大于"、"小于"、"等于"、"大于等于"、"小于等于"、"不等于"

    示例用法:

    #include <iostream>
    #include <string>
    using namespace std;
    
    void TrueOrFalse (int x)
    {
    cout << (x?"True":"False")<<endl;
    }
    
    int main ()
    {
    string S1 = "DEF";
    string CP1 = "ABC";
    string CP2 = "DEF";
    string CP3 = "DEFG";
    string CP4 ="def";
    cout << "S1= " << S1 << endl;
    cout << "CP1 = " << CP1 <<endl;
    cout << "CP2 = " << CP2 <<endl;
    cout << "CP3 = " << CP3 <<endl;
    cout << "CP4 = " << CP4 <<endl;
    cout << "S1 <= CP1 returned ";
    TrueOrFalse (S1 <=CP1);
    cout << "S1 <= CP2 returned ";
    TrueOrFalse (S1 <= CP2);
    cout << "S1 <= CP3 returned ";
    TrueOrFalse (S1 <= CP3);
    cout << "CP1 <= S1 returned ";
    TrueOrFalse (CP1 <= S1);
    cout << "CP2 <= S1 returned ";
    TrueOrFalse (CP2 <= S1);
    cout << "CP4 <= S1 returned ";
    TrueOrFalse (CP4 <= S1);
    cin.get();
    return 0;
    }
    

    输出结果:

    S1= DEF
    CP1 = ABC
    CP2 = DEF
    CP3 = DEFG
    CP4 = def
    S1 <= CP1 returned False
    S1 <= CP2 returned True
    S1 <= CP3 returned True
    CP1 <= S1 returned True
    CP2 <= S1 returned True
    CP4 <= S1 returned False
    

    字符串内容的修改

    1. 使用append()函数

      basic_string& append (const E * s); //在原始字符串后面追加字符串s
      basic_string& append (const E * s, size_type n);//在原始字符串后面追加字符串 s 的前 n 个字符
      basic_string& append (const basic_string& str, size_type pos,size_type n);//在原始字符串后面追加字符串 s 的子串 s [ pos,…,pos +n -1]
      basic_string& append (const basic_string& str);
      basic_string& append (size_type n, E c); //追加 n 个重复字符
      basic_string& append (const_iterator first, const_iterator last); //使用迭代器追加

    示例代码如下:

    string s1 = "123456";
    string s2 = "abcdefgh";
    
    string str;
    
    str.assign(s1);
    cout <<"after use assign: str = " << str << endl;
    
    1. 使用insert函数

      basic_string& insert (size_type p0 , const E * s); //插人 1 个字符至字符串 s 前面
      basic_string& insert (size_type p0 , const E * s, size_type n); // 将 s 的前 3 个字符插入p0 位置
      basic_string& insert (size_type p0, const basic_string& str);
      basic_string& insert (size_type p0, const basic_string& str,size_type pos, size_type n); //选取 str 的子串
      basic_string& insert (size_type p0, size_type n, E c); //在下标 p0 位置插入 n 个字符 c
      iterator insert (iterator it, E c); //在 it 位置插入字符 c
      void insert (iterator it, const_iterator first, const_iterator last); //在字符串前插入字符
      void insert (iterator it, size_type n, E c) ; //在 it 位置重复插入 n 个字符 c

    示例代码如下:

    string A("ello");
    string B ;
    B.insert(1,A);
    cout << B << endl;
    A = "ello";
    B = "H";
    B.insert (1,"yanchy ",3);
    cout<< B <<endl;
    A = "ello";
    B = "H";
    B.insert (1,A,2,2);
    cout << B << endl;
    A="ello";
    B.insert (1 , 5 , 'C');
    cout << B << endl;
    

    字符串替换函数

    方法原型:

    basic_string& replace (size_type p0, size_type n0, const E * s); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换
    basic_string& replace (size_type p0, size_type n0, const E *s, size_type n); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换 1 个字符
    basic_string& replace (size_type p0, size_type n0, const basic_string& str); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换
    basic_string& replace (size_type p0, size_type n0, const basic_string& str, size_type pos, size_type n); //使用串 str 的子串 str [pos, pos + n-1] 替换源串中的内容,从位置 p0 处开                            
    始替换,替换字符 n0 个
    basic_string& replace (size_type p0, size_type n0, size_type n, E c); //使用 n 个字符 'c' 替换源串中位置 p0 处开始的 n0 个字符
    basic_string& replace (iterator first0, iterator last0, const E * s);//使用迭代器替换,和 1) 用法类似
    basic_string& replace (iterator first0, iterator last0, const E * s, size_type n);//和 2) 类似
    basic_string& replace (iterator first0, iterator last0, const basic_string& str); //和 3) 类似
    basic_string& replace (iterator first0, iterator last0, size_type n, E c); //和 5) 类似
    basic_string& replace (iterator first0, iterator last0, const_iterator first, const_iterator last); //使用迭代器替换
    

    示例代码如下:

    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
    string var ("abcdefghijklmn");
    const string dest ("1234");
    string dest2 ("567891234");
    var.replace (3,3, dest);
    cout << "1: " << var << endl;
    var = "abcdefghijklmn";
    var.replace (3,1, dest.c_str(), 1, 3);
    cout << "2: " << var << endl;
    var ="abcdefghijklmn";
    var.replace (3, 1, 5, 'x');
    cout << "3: " << var << endl;
    string::iterator itA, itB;
    string::iterator itC, itD;
    itA = var.begin();
    itB = var.end();
    var = "abcdefghijklmn";
    var.replace (itA, itB, dest);
    cout << "4: " << var << endl;
    itA = var.begin ();
    itB = var.end();
    itC = dest2.begin () +1;
    itD = dest2.end ();
    var = "abodefghijklmn";
    var.replace (itA, itB, itC, itD);
    cout << "5: " << var << endl;
    var = "abcdefghijklmn";
    var.replace (3, 1, dest.c_str(), 4); //这种方式会限定字符串替换的最大长度
    cout <<"6: " << var << endl;
    return 0;
    }
    

    输出结果 :

    1: abc1234ghijklmn
    2: abc234efghijklmn
    3: abcxxxxxefghijklmn
    4: 1234
    5: 67891234efghijklmn
    6: abc1234efghijklmn
    

    字符串查找find

  • 相关阅读:
    理解JavaScript Call()函数原理。
    数据结构水题大赛官方题解#3 XXX的stl
    数据结构水题大赛官方题解#2 XXX的脑白金
    数据结构水题大赛官方题解#1 XXX的面试
    P3390 矩阵快速幂
    2020.6.8 T1 棋子移动
    U68862 奶牛滑迷宫
    5月18日考试错误题目走迷宫
    最小生成树两种算法详解
    p1220关路灯(小优化)
  • 原文地址:https://www.cnblogs.com/zuixime0515/p/10514183.html
Copyright © 2011-2022 走看看