zoukankan      html  css  js  c++  java
  • String类

    1、String类方法汇总

    函数名称 效果
    构造函数 产生或复制字符串
    =, assign 赋以新值
    swap 交换两个字符串的内容
    +=, qppend(), push_back() 添加字符
    insert() 插入字符
    erase() 删除字符
    clear() 移除全部字符
    resize() 改变字符数量
    raplace() 替换字符
    + 串联字符串
    ==, !=, <, <=, >, >=, compare() 比较字符串内容
    size(), length() 返回字符串数量
    max_size() 返回字符的最大可能个数
    empty() 判断字符串是否为空
    capacity() 返回重新分配之前的字符数量
    reserve() 保留内存以存储一定数量的字符
    [], at() 存取单一字符
    >>, getline() 从stream中读取某值
    << 将值写入stream
    copy() 将内容复制为一个C-string
    c_str 将内容以C-string形式返回
    data() 将内容以字符数组形式返回
    substr() 返回子字符串
    find() 搜寻某子字符串或字符
    begin(), end() 提供正向迭代器支持
    rbegin(), rend() 提供逆向迭代器支持
    get_allocator() 返回配置器
    析构函数 销毁字符串

    2、构造器和析构器

      

    1 //常见的string类构造函数
    2 string str;                     //生成空字符串
    3 string s(str);                  //生成字符串str的复制品
    4 string s(str, stridx);          //将字符串str中始于stridx的部分,作为构造函数的初值
    5 string s(str, strbegin, strlen);//将字符串str中始于strbegin、长度为strlen的部分作为字符串的初值
    6 string s(cstr);                 //以 C_string类型cstr作为字符串s的初值
    7 string s(cstr, char_len);       //以C_string类型cstr的前char_len个字符串作为字符串s的初值
    8 string s(num, c);               //生成一个字符串,包含num个c字符
    9 string s(str, beg, end);        //以区间[beg, end]内的字符作为字符串s的初值

      C_string一般认为是常规的C++字符串,目前存在一个从const char*到string是隐式转换,却不存在从string对象到C_string的自动类型转换,对于类型string类型的字符串,可以通过c_str()返回该string类对象对应的C_string。

      析构函数的形式如下:

      ~string()

    3、大小和容量

      对于string类的对象包含三种求解大小的函数:size()和length();max_size();capacity();

      (1)size()和length()。这两个函数会返回string对象中的字符个数;两个函数的执行效果相同。

      (2)max_size()。函数max_size返回string类对象最多包含的字符数。一旦程序使用长度超过max_size()的string操作,编译器会抛出length_error异常。

      (3)capacity()。重新分配内存之前,string对象能包含的最大字符数。

      string类还有一个函数reserve()。当调用该函数时,得以保留一定的预留容量。reserve()的默认容量为0;

     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 int main()
     5 {
     6     int size = 0;
     7     int length = 0;
     8     unsigned long maxsize = 0;
     9     int capacity = 0;
    10     string str("123456789");
    11     string str_custom;
    12     str_custom = str;
    13     size = str_custom.size();
    14     length = str_custom.length();
    15     maxsize = str_custom.max_size();
    16     capacity = str_custom.capacity();
    17     cout << "size = " << size << endl;
    18     cout << "length = " << length << endl;
    19     cout << "maxsize = " << maxsize << endl;
    20     cout << "capacity = " << capacity << endl;
    21     return 0;
    22 }
    1 size = 9
    2 length = 9
    3 maxsize = 1073741820
    4 capacity = 9

    4、元素存取(访问)

      访问单一字符的方法:

          下标操作符[]和成员函数at()。两者都返回指定索引的对应位置的字符。第1个字符索引(下标)为0,最后的字符索引为length - 1。

      两者的区别:

          下标操作符[]在使用时不会检查索引的有效性。如果下标超出字符的长度范围,会引起不必要的麻烦。对于常量字符串,使用下标运算符时,字符串的最后字符(即‘’)是有效的。对应string对象(常量型)最后一个字符的下标是有效的,调用返回''。

          函数at()会在使用时检查下标是否有效。如果给定的下标超出字符的长度范围,系统会抛出out_of_range异常。

    5、字符串比较

      字符串可以和类型相同的字符串相比较,也可以和具有同样字符类型的数组比较。basic_string类模板提供了比较运算符:>, <,==,>=,<=,!=等;还提供了方法compare()。对于方法compare(),如果相比较的两个字串相同,compare()函数返回0,否则返回非0值。

    1 //类basic_string的函数compare()的原型为:
    2 int compare(const basic_string& s)const;
    3 int compare(const Ch* p)const;
    4 int compare(size_type pos, size_type n, const basic_string& s)const;
    5 int compare(size_type pos, size_type n, const basic_string &s, size_type pos2, size_type n2)const;
    6 int compare(size_type pos, size_type n, const Ch* p, size_type n2 = npos)const;
     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     string A("aBcdef");
     8     string B("AbcdEf");
     9     string C("123456");
    10     string D("123dfg");
    11     
    12     int m = A.compare(B);               //完整的A和B的比较
    13     int n = A.compare(1, 5, B);         //"Bcdef"和"AbcdEf"的比较
    14     int p = A.compare(1, 5, B, 4, 2);   //“Bcdef”和"Ef"比较
    15     int q = C.compare(0, 3, D, 0, 3);   //“123”和“123”比较
    16     cout << "m = " << m << ", n = " << n << ", p = " << p << ", q = " << q << endl;
    17     cin.get();
    18     return 0;
    19 }

      string类的比较函数compare()使用非常方便,可以区分字母的大小写。

      当运用比较运算符比较字符串时,任何一个均不能为NULL。否则程序或异常退出。

    6、字符串的内容修改

      (1)assign函数

        assign函数可以直接给字符串赋值;既可以将整个字符串赋值给新串,也可以将字符串的字串赋值给新串。

        

    1 //其在basic_string类中的原型为:
    2 basic_string& assign(const char*s);                                         //直接使用字符串赋值
    3 basic_string& assign(const char*s, size_type n);                            //限定被初始化的字符串长度
    4 basic_string& assign(const basic_string& str, size_type pos, size_type n);  //将str的字串赋值给调用串
    5 basic_string& assign(const basic_string& str);                              //使用字符串的"引用"赋值
    6 basic_string& assign(size_type n, char c);                                  //使用n个重复字符赋值
    7 basic_string& assign(const_iterator first, const_iterator last);            //使用迭代器赋值

      (2)operator=

        “operator=”的功能就是赋值。

      (3)函数erase()

    1 //函数原型为:
    2 iterator erase(iterator first, iterator last);
    3 iterator erase(iterator it);
    4 basic_stirng& erase(size_type p0 = 0, size_type n = npos);
    5 //函数的使用方法
    6 str.erase(str.begin(),str.end());
    7 str.erase(3);
    (1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
    (2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
    (3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)

      (4)swap()函数

    1 //函数原型为
    2 void swap(basic_string& str);
    3 //函数的使用方法
    4 string str2("abcdefghijklmn");
    5 str.swap(str2);

      (5)insert()函数

        

     1 //函数原型为
     2 basic_string& insert(size_type p0, const char* s);
     3 basic_string& insert(size_type p0, const char* s, size_type n);
     4 basic_string& insert(size_type p0, const basic_string& str);
     5 basic_string& insert(size_type p0, const basic_string& str, size_type pos, size_type n);
     6 
     7 basic_string& insert(size_type p0, size_type n, char c);
     8 iterator insert(iterator it,char c);
     9 void insert(iterator it, const_iterator first, const_iterator last);
    10 void insert(iterator it, size_type n, char c);

       (6)append()函数

    1 //函数原型为
    2 basic_string& append(const char* s);                                        //在原始字符串后面追加字符串s
    3 basic_string& append(const char* s, size_type n);                           //在原始字符串后面追加字符串s的前n个字符
    4 basic_string& append(const basic_string& str, size_type pos, size_type n);  //在原始字符串后面追加字符串s的字串s[pos, ..., pos+n-1]
    5 basic_string& append(const basic_string& str);                              //在原始字符串之后追加字符串str
    6 basic_string& append(size_type n, char c);                                  //追加n个重复字符
    7 basic_string& append(const_iterator first, const_iterator last);            //使用迭代器追加

    7、字符串内容的替换

      replace()函数

     1 //函数原型为
     2 basic_string& replace(size_type p0, size_type n0, const char* s);               
     3 basic_string& replace(size_type p0, size_type n0, char* s, size_type n);
     4 basic_string& replace(size_type p0, size_type n0, const basic_string& str);
     5 basic_string& replace(size_type p0, size_type n0, const basic_string& str, size_type pos, size_type n);
     6 basic_string& replace(size_type p0, size_type n0, size_type n, char c);
     7 
     8 basic_string& replace(iterator first0, iterator last0, const char* s);
     9 basic_string& replace(iterator first0, iterator last0, const char* s, size_type n);
    10 basic_string& replace(iterator firat0, iterator last0, const basic_string& str);
    11 basic_string& replace(iterator first0, iterator last0, size_type n, char c);
    12 basic_string& replace(iterator first0, iterator last0, const_iterator first, const_iterator last);
     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     string var("abcdefghijklmn");
     8     const string dest("1234");
     9     string dest2("567891234");
    10     var.replace(3, 3, dest);
    11     cout << "1: " << var << endl;
    12     /////////////////////////////////////
    13 
    14     var = "abcdefghijklmn";
    15     var.replace(3, 1, dest.c_str(), 1, 3);
    16     cout << "2: " << var << endl;
    17     ////////////////////////////////////
    18 
    19     var = "abcdefghijklmn";
    20     var.replace(3, 1, 5, 'x');
    21     cout << "3: " << var << endl;
    22     ///////////////////////////////////
    23 
    24     string::iterator itA, itB;
    25     string::iterator itC, itD;
    26     itA = var.begin();
    27     itB = var.end();
    28     var = "abcdefghijklmn";
    29     var.replace(itA, itB, dest);
    30     cout << "4: " << var << endl;
    31     ////////////////////////////////////
    32 
    33     itA = var.begin();
    34     itB = var.end();
    35     itC = dest2.begin() + 1;
    36     itD = dest2.end();
    37     var = "abcdefghijklmn";
    38     var.replace(itA, itB, itC, itD);
    39     cout << "5: " << var << endl;
    40     ///////////////////////////////////
    41 
    42     var = "abcdefghijklmn";
    43     var.replace(3, 1, dest.c_str(), 4);
    44     cout << "6: " << var << endl;
    45     return 0;
    46 }

     8、字符串连接

      可以使用operator+将两个字符串拼接起来。

    9、字符串I/O操作

      operator<<和oparator>>提供了C++语言的字符串输入/输出功能。operator<<可以实现将字符写入一个流中;operator>>可以实现将以空白或回车为结束的完整的字符读入到对应的字符串中,开头和结尾的空白字符不包括进字符串中。

      还有一个常用的函数getline(),函数包括两种形式:

    10、字符串搜索

      关于npos:npos是一个无符号的整数值,初始值为-1.当搜索失败时,npos表示“没有找到(not found)”或“所有剩余字符串”。

      如果查找函数find()和其他函数没有搜索到期望的字符(或字串),函数返回npos;如果搜索成功,则返回搜索到的第1个字符或字串的位置。

      1、find()函数和rfind()函数

        find()函数的原型主要有四种。

    1 size_type find(value_type _Ch, size_type _Off = 0) const;
    2 //代码中find()函数的第1个参数是被搜索的字符,第2个参数是在源串中开始搜索的下标位置
    3 size_type find(const value_type* _Ptr, size_type _Off = 0)const;
    4 //find()函数中的第1个参数是被搜索的字符串,第2个参数是在源串中开始搜索的下标位置。
    5 size_type find(const value_type* _Ptr, size_type _Off = 0, size_type _Count)const;
    6 //第1个参数是被搜索的字符串,第2个参数是源串中开始搜索的下标,第3个参数是关于
    7 //第1个参数的字符个数,可能是_Ptr的所有字符数,也可能是_Ptr的子串字符个数。
    8 size_type find(const basic_string& _Str, size_type _Off = 0)const;
    9 //函数的第1个参数是被搜索的字符串,第2参数是在源串中开始搜索的下标位置

      rfind函数的原型和find()函数的原型类似。(反向查找)

      2、函数find_first_of()和函数find_last_of()

        函数find_first_of()实现在源串中搜索某字符串的功能,函数返回值是被搜索字符串的第1个字符第1次出现的下标(位置)。如果查找失败,函数返回npos;

     1 //函数的原型为
     2 size_type find_first_not_of(value_type _Ch, size_type _Off = 0)const;
     3 size_type find_first_of(const value_type* _Ptr, size_type _Off = 0)const;
     4 size_type find_first_of(const value_type* _Ptr, size_type _Off, size_type _Count)const;
     5 size_type find_first_of(const basic_string& _Str, size_type _Off = 0)const;
     6 
     7 
     8 size_type find_last_of(value_type _Ch, size_type _Off = npos)const; 
     9 size_type find_last_of(const value_type* _Ptr, size_type _Off = npos)const;
    10 size_type find_last_of(const value_type* _Ptr, size_type _Off, size_type _Count)const;
    11 size_type find_last_of(const basic_string& _Str, size_type _Off = npos)const;

      3、函数find_first_not_of()和函数find_last_not_of()

    1 //函数find_first_not_of()的函数原型为:
    2 size_type find_first_not_of(value_type _Ch, size_type _Off = 0)const;
    3 size_type find_first_not_of(const value_type* _Ptr, size_type _Off = 0)const;
    4 size_type find_first_not_of(const value_type* _Ptr, size_type _Off, size_type _Count)const;
    5 size_type find_first_not_of(const basic_string& _Str, size_type _Off = 0)const;

      find_first_not_of()函数实现在源字符串中搜索与指定字符(串)不相等的第一个字符。

  • 相关阅读:
    [Swift]GZip字符串压缩和解压缩(Java/C#通用)
    [XCode]UI测试/单元测试
    转 oracle apex 使用
    转 pygame学习笔记(1)——安装及矩形、圆型画图
    转 11g RAC R2 体系结构---Grid
    转如何升级oracle版本?(11.2.0.1至11.2.0.4)
    ORA-14074: partition bound must collate higher than that of the last partition
    12c pdb expdp use DATA_PUMP_DIR meet ORA-39145
    转【Python】Python-skier游戏[摘自.与孩子一起学编程]
    Dock
  • 原文地址:https://www.cnblogs.com/CZT-TS/p/7609143.html
Copyright © 2011-2022 走看看