C++ STL String类(2)

C++ STL String类(2)

string内容的修改

一般来说 有很多种方法可以去修改string,例如assign(),operator=,erase(),swap(),insert(),append()等等;

assign
这个函数可以直接给string再重新赋值,例如:

#include <iostream>
#include <string>
using namespace std;
int main(){
    string str1 ("123456");
    string str;
    str.assign(str1);  //直接调用assign去把str1赋值给str
    cout<<str<<endl;
    str.assign(str1,3,3);
    cout<<str<<endl;
    str.assign(str1,2,str1.npos);
    string::iterator itBegin;
    string::iterator itEnd;
    itBegin = str1.begin();
    itEnd = str1.end();
    str.assign(itBegin,--itEnd);
    cout<<str<<endl;
}

输出为:

123456
456
3456
12345

erase()
擦除函数,删除对应string上的元素;

str.erase (str* begin(), str.end());
str.erase (3);
str.swap (str2);

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;
B.insert(1,"nihaoya",3);
cout<<B<<endl;
B.insert (1,A,2,2);
cout<<B<<endl;

find()、rfind()
若查找 find() 函数和其他函数没有搜索到期望的字符(或子串),则返回 npos;若搜索成功,则返回搜索到的第 1 个字符或子串的位置。其中,npos 是一个无符号整数值,初始值为 -1。当搜索失败时, npos 表示“没有找到(not found)”或“所有剩佘字符”。
其中find()是顺序查找,而rfind()是逆序查找;

size_type find (value_type _Chr, size_type _Off = 0) const;
//find()函数的第1个参数是被搜索的字符、第2个参数是在源串中**开始搜索的下标位置**
size_type find (const value_type* _Ptr , size_type _Off = 0) const;
//find()函数的第1个参数是被搜索的字符串,第2个参数是在源串中开始搜索的下标位置
size_type find (const value_type* _Ptr, size_type _Off = 0, size_type _Count) const;
//第1个参数是被搜索的字符串,第2个参数是源串中开始搜索的下标,第3个参数是关于第1个参数的字符个数,可能是 _Ptr 的所有字符数,也可能是 _Ptr 的子串宇符个数
size_type find (const basic_string& _Str, size_type _Off = 0) const;
//第1个参数是被搜索的字符串,第2参数是在源串中开始搜索的下标位置

find_first_of()、 find_last_of()
这两个函数主要是实现了在源字符串中搜索字符串的功能,然后他会返回其中找到的第一个字符串的index。若查询失败,则返回npos。
其中find_first_of是正向,find_last_of是反向,也就是最后一个匹配上的。

size_type find_first_not_of (value_type_Ch, size_type_Off = 0) const; size_type find_first_of (const value_type* _Ptr, size_type _Off = 0) const;
size_type find_first_of (const value_type* _Ptr, size_type_Off, size_type_Count) const;
size_type find_first_of (const basic_string & _Str, size_type_Off = 0) const;
size_type find_last_of (value_type _Ch, size_type_Off = npos) const;
size_type find_last_of (const value_type* _Ptr, size_type_Off = npos) const;
size_type find_last_of (const value_type* _Ptr, size_type _Off, size_type _Count) const;
size_type find_last_of (const basic_string& _Str, size_type_Off = npos) const;

find_first_not_of()、find_last_not_of()
这两个函数是查找的字符串中和目标字符不匹配的第一个字符、或者最后一个字符。。

size_type find_first_not_of (value_type _Ch, size_type_Off = 0) const;
size_type find_first_not_of (const value_type * _Ptr, size_type_Off = 0) const;
size_type find_first_not_of (const value_type* _Ptr, size_type_Off, size_type_Count) const;
size_type find_first_not_of (const basic_string & _Str, size_type_Off = 0) const;

C++ string 支持迭代器

迭代器是STL的精髓,STL中一共有五种迭代器,分别是输入迭代器、输出迭代器、正向迭代器、双向迭代器以及随机迭代器.
其中std::string实现了常规的迭代器和反向迭代器。其中提供的具体函数主要有:
begin() end() rbegin() rend() append() assign() insert() erase() replace()等等。

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main ()
{
    string s ("The zip code of Hondelage in Germany is 38108.");
    cout << "Original: " << s << endl;
    string sd(s.begin(),s.end ()); //构造函数中使用迭代器
    cout << "Destination: " << sd << endl;
    transform (sd.begin(), sd.end(), sd.begin(), toupper); //算法中使用迭代器(仿函数)
    cout << "Destination (All Toupper)): " << sd << endl;
    string sd1;
    sd1.append (sd.begin(),(sd.end() -7)); //append()函数中使用迭代器
    cout << "Destination sd1: " << sd1 << endl;
    string sd2;
    string::reverse_iterator iterA;
    string temp = "0";
    for (iterA = sd.rbegin (); iterA != sd.rend (); iterA++) //reverse_iterator
    {
        temp=* iterA;
        sd2.append (temp);
    }
    cout << "Destination sd2: " << sd2 << endl;
    sd2.erase (0, 15); //erase()函数中使用迭代器
    cout << "Destination sd2 (Erased 15 chars) : " << sd2 << endl;
    string::iterator iterB = sd2.begin ();
    string sd3 = string ("12345678");
    sd2.insert (sd2.begin(), sd3.begin(), sd3.end()); //insert()函数中使用迭代器
    cout << "Destination sd2 (Insert 8 chars) : " << sd2 << endl;
    sd2.replace (sd2.begin (), sd2.end(), "This is an Exarrple of Replace"); //Replace
    cout <<"Destination sd2 (Replace All): " << sd2 << endl; // replace ()函数中使用迭代器
}

对应的输出为:

Original: The zip code of Hondelage in Germany is 38108.
Destination: The zip code of Hondelage in Germany is 38108.
Destination (All Toupper)): THE ZIP CODE OF HONDELAGE IN GERMANY IS 38108.
Destination sd1: THE ZIP CODE OF HONDELAGE IN GERMANY IS
Destination sd2: .80183 SI YNAMREG NI EGALEDNOH FO EDOC PIZ EHT
Destination sd2 (Erased 15 chars) : EG NI EGALEDNOH FO EDOC PIZ EHT
Destination sd2 (Insert 8 chars) : 12345678EG NI EGALEDNOH FO EDOC PIZ EHT
Destination sd2 (Replace All): This is an Exarrple of Replace
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值