string类方法介绍

字符串存取

    有4种方法可以访问各个字符,其中两种方法使用[]操作符,另外两种方法使用at()方法:

 

reference operator[] (size_type pos);
const_reference operator[] (size_type pos) const;
reference at (size_type n);
const_reference at (size_type n) const;

 

    第一个operator[]()方法使得能够使用数组表示法来访问字符串的元素,可用于检索或更改值。

    第二个operator[]()方法可用于const对象,只能用于检索值。

 

    at()方法提供了相似的访问功能,只是索引是通过函数参数提供的:

 

string word ("tack");
cout << word.at(0);
           // display the t

 

    at()方法执行边界检查,如果pos >= size(),将引发out_of_range异常。pos的类型为size_type,是无符号的,因此pos的值不能为负;而operator[]()方法不进行边界检查,因此,如果pos >= size(),则其行为将是不确定的,如果pos == size(),const版本将返回空值字符的等价物。

 

返回原始字符串的子字符串

 

    basic_string substr (size_type pos = 0, size_type n = npos)const;

 

    该方法返回一个字符串——这是从pos开始,复制n个字符(或到字符串尾部)得到的。例如,下面的代码将pet初始化为"donkey":

string message("Maybe the donkey will learn to sing.");
string pet(message.substr(10,6) );

 

基本赋值

    有3种重载的赋值方法:

basic_string & operator= (const basic_string & str);
basic_string & operator= (const charT * s);
basic_string & operator= (charT c);

 

    第一种方法将一个string对象赋给另一个;

    第二种方法将C-风格的字符串赋给string对象;

    第三种方法将一个字符赋给string对象。

 

字符串搜索

    string类提供了6种搜索函数find()、find()、find_first_of()、find_last_of()、find_first_not_of()find_last_not_of(),其中每个函数都有4个原型。

 

    详细介绍请查看<<string类和常用方法>>

 

比较方法和函数

    string类提供了用于比较2个字符串的方法和函数,原型如下:

int compare(const basic_string & str)const;

int compare(size_type pos1, size_type n1,const basic_string & str)const;

int compare(size_type pos1, size_type n1,const basic_string & str,
                size_type pos2, size_type n2)const;

int compare(const charT * s) const;

int compare(size_type pos1, size_type n1,const charT * s, size_type n2 = npos)const;

 

    这些方法使用traits::compare()方法,后者是为用于字符串的字符类型定义的。如果根据traits::compare()提供的顺序,第一个字符串位于第二个字符串之前,则第一个方法将返回一个小于0的值;如果这两个字符串相同,则返回0;如果第一个字符串位于第二个字符串的后面,则返回一个大于0的值。如果较长的字符串的前半部分与较短的字符串相同,则较短的字符串将位于较长的字符串之前。

 

string s1("bellflower");
string s2("bell");
string s3("cat");
int al3 = s1.compare (s3);    // al3 is < 0
int al2 = s1.compare (s2);
    // al2 is > 0

 

    第二个方法与第一个方法相似,不过它进行比较时,只使用第一个字符串中从位置pos1开始的n1个字符。

下例将字符串s1的前4个字符同字符串s2进行比较:
string s1("bellflower");
string s2("bell");
int a2 = s1.compare (0,4,s2);
  // a2 is 0

 

    第三个方法与第一个方法相似,不过它使用第一个字符串中从位置pos1开始的n1个字符和第二个字符串中从pos2位置开始的n2个字符进行比较。

下例将对stout中的out何about中的out进行比较:
string st1("stout boar");
string st2("mad about ewe");
int a3 = s1.compare (2,3,st2,6,3);
  // a3 is 0

 

    第四个方法与第一个方法相似,不过它将一个字符串数组而不是string对象作为第二个字符串。
    第五个方法与第三个方法相似,不过它将一个字符串数组而不是string对象作为第二个字符串。

 

非成员比较函数

    重载的关系操作符:

operator==()
operator<()
operator<=()
operator>()
operator>=()
operator!=()

 

    每一个操作符都被重载,使之将string对象与string对象进行比较、将string对象与字符串数组进行比较、将字符串数组与string对象进行比较。它们都是根据compare()方法定义的,因此提供了一种在表示方面更为方便的比较方式。

 

字符串修改方法

用于追加和相加的方法

    可以使用重载的+=操作符或append()方法将一个字符串追加到另一个字符串的后面。如果得到的字符串长于最大字符串长度,将引发length_error异常。+=操作符使得能够将string对象、字符串数组或单个字符追加到string对象的后面:

 

basic_string & operator+=(const basic_string & str);
basic_string & operator+=(const charT * s);
basic_string & operator+=(charT c);

 

    append()方法功能一样。此外,通过指定初始位置和追加的字符数,或者通过指定区间,还可以追加string对象的一部分。通过指定要使用字符串中的多少个字符,可以追加字符串的一部分。追加字符的版本使得能够指定要复制该字符的多个实例。

 

append()方法的原型:

basic_string & append(const basic_string & str);
basic_string & append(const basic_string & str, size_type pos,
                  size_type n);
template<class InputIterator>
   basic_string & append(InputIterator first, InputIterator last);
basic_string & append(const charT * s);
basic_string & append(const charT * s, size_type n);
basic_string & append(size_type n, charT c);
    // append n copies of c

 

范例:
string test("The");
test.append("ory");
    // test is "Theory"
test.append(3,'!');   // test is "Theory!!!"

 

    operator+()函数被重载,以便能够拼接字符串。该重载函数不修改字符串,而是创建一个新的字符串,该字符串是通过将第二个字符串追加到第一个字符串后面得到的。加法函数不是成员函数,它们使得能够将string对象和string对象、string对象和字符串数组、字符串数组和string对象、string对象和字符以及字符和string对象相加。

 

其他赋值方法

    assign()方法使得能够将整个字符串、字符串的一部分或由相同字符组成的字符序列赋给string对象。

该方法的原型:

basic_string & assign(const basic_string &);
basic_string & assign(const basic_string & str, size_type pos,
                   size_type n);
basic_string & assign(const charT * s, size_type n);
basic_string & assign(const charT * s);
basic_string & assign(size_type n, charT c); 
   // append n copies of c
template<class InputIterator>
   basic_string & assign(InputIterator first, InputIterator last);

 

范例:
string test;
string stuff("set tubs clones ducks");
test.assign(stuff,1,5);       // test is "et tu"
test.assign(6,'#'); 
          // test is "######"

 

插入方法

    insert()方法使得能够将string对象、字符串数组或几个字符插入到string对象中。该方法与append()方法相似,不过它还接受另一指定插入位置的参数,该参数可以是位置,也可以是迭代器。数据将被插入到插入点的前面。有几种方法返回一个指向得到的字符串的引用。如果pos1超过了目标字符串结尾,或者pos2超过了要插入的字符串结尾,将引发out_of_range异常。如果得到的字符串长与最大长度,将引发length_error异常。

该方法的原型:

basic_string & insert(size_type pos1, const basic_string & str);
basic_string & insert(size_type pos1, const basic_string & str,
                  size_type pos2, size_type n);
basic_string & insert(size_type pos, const charT * s, size_type n);
basic_string & insert(size_type pos, const charT * s);
basic_string & insert(size_type pos, size_type n, charT c);
iterator insert(iterator p, charT c = charT());
void insert(iterator p, size_type n, charT c);
template<class InputIterator>
   void insert(iterator p, InputIterator first, InputIterator last);

 

例如,将字符串"former "字符串插入到"The banker."中b的前面:
string st3("The banker.");
st3.insert(4,"former ");

 

将字符串" waltzed"(不包括!,这是第9个字符)插入到"The former banker."结尾的句号之前:
st3.insert(st3.size()-1," waltzed! ",8);

 

清除方法

    erase()方法从字符串中删除字符。原型如下:

basic_string & erase(size_type pos=0, size_type n=npos);
iterator erase(iterator position);
iterator erase(iterator first, iterator last);

 

    第一种格式将从pos位置开始,删除n个字符或删除到字符串尾。
    第二种格式删除迭代器位置引用的字符,并返回指向下一个元素的迭代器;如果后面没有元素,则返回end()。
    第三种格式删除区间[first,last)中的字符,即从first(包括)到last(不包括)之间的字符;它返回最后一个迭代器,该迭代器指向最后一个被删除的元素后面的一个元素。

 

替换方法

    replace()方法指定了要替换的字符串部分和用于替换的内容。可以使用初始化位置和字符数目或迭代器区间来指定要替换的部分。替换内容可以是string对象、字符串数组,也可以是特定字符的多个实例。对于用于替换的string对象和数组,可以通过指定特定部分(使用位置和计数或只使用计数)或迭代器区间做进一步的修改。

该方法的原型:

basic_string & replace(size_type pos1, size_type n1, const basic_string & str);
basic_string & replace(size_type pos1, size_type n1, const basic_string & str,
 size_type pos2, size_type n2);
basic_string & replace(size_type pos1, size_type n1, const charT * s, size_type n2);
basic_string & replace(size_type pos,size_type n1,const charT * s);
basic_string & replace(size_type pos,size_type n1,size_type n2,charT c);
basic_string & replace(iterator i1, iterator i2,const basic_string & str);
basic_string & replace(iterator i1, iterator i2,const charT * s,size_type n);
basic_string & replace(iterator i1, iterator i2, const charT * s);
basic_string & replace(iterator i1, iterator i2,size_type n, charT c);
template<class InputIterator>
    basic_string & replace(iterator i1, iterator i2,InputIterator j1, InputIterator j2);

 

范例:

string test("Take a right turn at Main Street.");
test.replace(7,5,"left");
  // replace "right" with "left"

 

可以使用find()来找出要在replace()中使用的位置:

string s1 = "old";
string s2 = "mature";
string s3 = "The old man and the sea";
string::size_type pos = s3.fine(s1);
if (pos != stirng::npos)
   s3.replace(pos,s1.size(),s2);

上述代码将old替换为mature 。

 

其他修改方法

    copy()方法将string对象或其中一部分复制到指定的字符串数组中:

    size_type copy(charT * s, size_type n, size_type pos = 0)const;

 

    其中,s指向目标数组,n是要复制的字符数,pos指出从string对象的什么位置开始复制。复制将一直进行,直到复制了n个字符或到达string对象的最后一个字符。


    函数返回复制的字符数,该方法不追加空值字符,也不检查目标数组的长度是否足够。

 

    swap()方法使用一个恒定时间的算法来交换来按个string对象的内容:

 

    void swap(basic_string<charT, traits, Allocator> &);

 

输入和输出

    重载的<<操作符用来输出。>>操作符和getline()函数用来输入。

 

    详细介绍请查看<<string类和常用方法>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值