String基本字符型序列容器

String基本字符型序列容器

一、原理

      字符串由一个个字符组成,字符分为单字节char,和双字节wchar_t两种。string被typedef定义为basic_string<char>,因此,string与basic_string有相同的成员变量和函数。

二、应用

char* cArray="abc";

string s(cArray);

string s2(s);

此外,还有和前面容器一样的insert,append函数来对字符或字符串的添加。有访问的前向迭代器和后向迭代器。还有字符的删除等函数。

1、string(const string& s,size_type pos=0,size_type n=pos)

拷贝构造函数,将string对象s的[pos,pos+n)中字符拷贝到新创建的string对象空间中,其中string::npos已定义为-1,当pos取值为0,n取值为string::npos时,就是用整个字符串s创建一个新的string对象。

2、替换

string& replace(size_type p0, size_type n0,const E *s);

将当前字符串从p0开始位置的n0字符,替换为字符串s

string& replace(size_type p0, size_type n0,const string& str);

将当前字符串从p0开始位置的n0字符,替换为字符串str

string& replace(iterator first0, iterator last0,const E *s);

string& replace(iterator first0, iterator last0,const string& str);

将当前字符串[first,last)区间中的字符串替换为字符串str

MSDN中有共多的叙述。

3、查找

size_type find(E c, size_type pos = 0) const;

在当前字符串的pos位置开始查找字符c,返回索引位置

size_type find(const E *s, size_type pos = 0) const;

size_type find(const E *s, size_type pos, size_type n) const;

size_type find(const string& str, size_type pos = 0) const;

size_type find_first_of(E c, size_type pos = 0) const;

size_type rfind(E c, size_type pos = npos) const;

在当前字符串的pos位置开始反向查找字符c,返回索引位置

size_type rfind(const E *s, size_type pos = npos) const;

size_type rfind(const E *s, size_type pos, size_type n = npos) const;

size_type rfind(const basic_string& str,size_type pos = npos) const;

size_type find_first_of(const E *s, size_type pos = 0) const;

在当前字符串的pos位置开始查找第一个与子串s的任何一个字符相同的字符的位置,返回找到字符的索引位置。

size_type find_first_of(const E *s, size_type pos, size_type n) const;

在当前字符串的[pos,pos+n]位置开始查找第一个与子串s的任何一个字符相同的字符的位置,返回找到字符的索引位置。

size_type find_first_not_of(E c, size_type pos = 0) const;

size_type find_first_not_of(const E *s, size_type pos = 0) const;

在当前字符串的pos位置开始查找与子串s中任何一个字符都不相同的第一个字符,返回找到字符的索引位置,找不到返回npos。

size_type find_first_not_of(const E *s, size_type pos,size_type n) const;

size_type find_first_not_of(const basic_string& str,size_type pos = 0) const;

+++++++++++++++++++++++++++++++++++++++

size_type find_last_not_of(E c, size_type pos = npos) const;

size_type find_last_not_of(const E *s, size_type pos = npos) const;

在当前字符串的pos位置开始查找最后一个与子串s中任何一个字符都不相同的字符,返回找到字符的索引位置。

size_type find_last_not_of(const E *s, size_type pos,  size_type n) const;

size_type find_last_not_of(const basic_string& str,size_type pos = npos) const;

4、比较

int compare(const basic_string& str) const;

将当前字符串与字符串str比较

int compare(size_type p0, size_type n0,const string& str);

将当前字符串从p0开始的n0个字符构成的字符串与字符串str比较

5、字符的添加

string& append(const E *s);

string& append(const E *s, size_type n);

string& append(const string& str,size_type pos, size_type n);

string& append(const string& str);

在string对象的字符串尾部添加str对象的字符串

string& append(size_type n, E c);

string& append(const_iterator first, const_iterator last);

++++++++++++++++++++++++++++++++++++++++++++++

string& insert(size_type p0, const E *s);

string& insert(size_type p0, const E *s, size_type n);

string& insert(size_type p0,const string& str);

string& insert(size_type p0,const string& str, size_type pos, size_type n);

string& insert(size_type p0, size_type n, E c);

在string对象的p0位置,插入一个字符c

iterator insert(iterator it, E c);

在string对象的it位置之前,插入一个字符c

void insert(iterator it, const_iterator first, const_iterator last);

void insert(iterator it, size_type n, E c);

在string对象的it位置之前,插入一个字符c

    此外,还有length,size,empty等,c_str()将当前string对象转换成c字符数组。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值