string构造函数
string s 生成一个空字符串s
string s(str) 拷贝构造函数,生成str对象的复制品
string s(str,stridx) 将字符串str对象内"始于位置stridx"的部分当作字符串的初值
string s(str,stridx,strlen) 将字符串str对象始于stridx且最长为strlen部分作为字符串的初值
string s(cstr) 将C字符串作为s的初值
string s(chars,chars_len) 将C字符串前chars_len个字符作为字符串s的初值
string s(num,c) 生成一个字符串,包含num个c字符
string s(beg,end) 以区间(迭代器iterator )beg到end(不包含end)内的字符作为字符串s的初值
string析构函数
s.~string() 销毁所有字符,释放内存
赋值操作
重载=操作符
basic_string& operator=(const basic_string& rhs)
basic_string& operator=(const E *s)
basic_string& operator=(E c)
string对象= string对象 / 字符串("gaint") / 字符( ' j ')
assign()函数
s.assign(str) 把str对象赋给s对象
s.assign(str,pos,n) 把string 对象中从(下标)pos的后n个元素赋值给s
s.assign(str,pos,string::npos) 把字符串str从索引值p开始到结尾赋给s
s.assign(char_string) 把字符串赋值给s对象
s.assign(char_string,num) 把字符串前num个字符'赋给s对象
s.assign(first,last) 把(迭代器iterator )first和last之间的部分赋给字符串
s.assign(num,char_x) 把num个字符赋给s对象
交字符对象内容
void swap(basic_string& str) 把s与str对象的内容交换
尾部添加字符
重载+= 操作符
basic_string& operator+=(const basic_string& rhs)
basic_string& operator+=(const E *s)
basic_string& operator+=(E c)
string对象+= string对象 / 字符串("gaint") / 字符( ' j ')
append()函数
s.append(str) 把str对象t添加到s对象末尾
s.append(str,pos,num) 把string对象中从(下标)pos的后num个元素添加到s对象末尾
s.append(char_string) 把字符串添加到s对象末尾
s.append(char_string.num) 把字符串的前num个字符添加到s对象末尾
s.append(first,last) 把(迭代器iterator )first到last中的元素添加到s末尾
s.append(num,char_x) 把num个字符添加到s对象末尾
push_back()函数
s.push_back(char_x) 把单个字符添加到s的尾部,这个函数只能增加单个字符
插入字符
insert()函数
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)
basic_string& insert(size_type p0,const E *s, size_type n)
basic_string& insert(size_type p0, const E *s)
basic_string& insert(size_type p0, size_type n, E c)
iterator insert(iterator it, E c)
void insert(iterator it, size_type n, E c)
void insert(iterator it,const_iterator first, const_iterator last)
删除字符 erase() 函数
basic_string& erase(size_type p0 = 0, size_type n = npos)
iterator erase(iterator it)
iterator erase(iterator first, iterator last)
转换为字符串数组
const E *c_str() const 将内容以C_string返回,一个以'\0'结尾的字符数组
替换字符
s.replace(pos ,num , str ) 用str代替s中从pos开始的num个字符
s.replace(pos ,num , chars) 用字符串代替s中从pos开始的num个字符
s.replace(pos1,num1,str,pos2, num2) 用str中从pos2开始的num2个字符,代替s中从pos1开始的 num1个字符
s.replace(pos1,num1,chars, num2 ) 用字符串中num2个字符,代替s中从pos1开始的num1个字符
s. replace(pos,num,count,char) 用count个字符,代替s中从 pos开始的num个字符
s.replace(First,Last,str ) 用 string 代替s中从(迭代器iterator)First到Last的字符
s.replace(First,Last,chars) 用字符串代替s中从(迭代器iterator)First到Last的字符
s.replace(First0,Last0,chars, num ) 用字符串的num个字符,代替s中从(迭代器iterator )First到Last的字符
s.replace(First0,Last0,First,Last ) 用(迭代器iterator)First开始到Last的字符,代替s中从(迭代器iterator )First0到Last0的字符
s.replace(First,Last,count,char) 用count个字符,代替s中从(迭代器iterator )First到Last的字符
比较字符串
==
!=
<
<=
>
>=
支持string与c_string的比较(如 str<"hello"),字典排序靠前的字符小,比较的顺序是从前向后比较,遇到不相等的字符就按这个位置上的两个字符的比较结果确定两个字符串的大小。同时,string ("aaaa") <string(aaaaa),compare()他支持多参数处理,支持用索引值和长度定位子串来进行比较。他返回一个整数来表示比较结果,返回值意义如下:0表相等 大于0表大于 小于0表小于
int compare(const basic_string& str) const;
int compare(size_type p0, size_type n0,const basic_string& str
int compare(size_type p0, size_type n0,const basic_string& str, size_type pos, size_type n)
int compare(size_type p0, size_type n0,const E *s) const
int compare(const E *s) const
int compare(size_type p0, size_type n0,const E *s, size_type pos) const
字符串长度
size_type size() const
size_type length() const
返回字符数量,他们等效
字符串最大个数
size_type max_size() const 返回字符串的可能最大个数,C++字符串最多能包含的字符数,很可能和机器本身的限制或者字符串所在位置连续内存的大小有关系
字符串是否为空
booempty() const 判断字符串是否为空
重新指定字符数组长度
void resize(size_type n, E t = E()) 重新指定字符数组长度的长度为n,元素值为t
新分配之前字符容量
size_type capacity() const 返回重新分配之前的字符容量
保留一定量内存以容纳一定数量的字符
void reserve(size_type n = 0) 保留一定量内存以容纳一定数量的字符,这个函数为string重新分配内存,重新分配的大小由其参数决定,默认参数为0,这时候会对string进行非强制性缩减
存取单一字符
const_reference at(size_type pos) const
reference at(size_type pos)
const_reference operator[](size_type pos) const
reference operator[](size_type pos)
从stream读取某值
>>
getline()
从stream读取某值
将谋值写入stream
<< 将谋值写入stream
复制string
size_type copy(E *s,size_type n, size_type pos = 0) const 将某值赋值为一个C_string,把字符串的内容复制或写入既有的c_string或字符数组内
返回字符数组形式
const E *data() const 将内容以字符数组形式返回,但并不添加’\0’
返回某个子字符串
basic_string substr(size_type pos = 0, size_type n = npos) const
搜索与查找
这些函数返回符合搜索条件的字符区间内的第一个字符的索引,没找到目标就返回npos.所有的函数的参数说明如下:第一个参数是被搜寻的对象.第二个参数(可有可无)指出string内的搜寻起点索引,第三个参数(可有可无)指出搜寻的字符个数
size_type find(const basic_string& str,size_type pos = 0) const
size_type find(const E *s, size_type pos, size_type n) const
size_type find(const E *s, size_type pos = 0) const
size_type find(E c, size_type pos = 0) const
size_type find(E c, size_type pos = 0) const
size_type rfind(const basic_string& str,size_type pos = npos) const
size_type rfind(const E *s, size_type pos,size_type n = npos) const
size_type rfind(const E *s, size_type pos = npos) const
size_type rfind(E c, size_type pos = npos) const
size_type find_first_of(const basic_string& str,size_type pos = 0) const
size_type find_first_of(const E *s, size_type pos,size_type n) const
size_type find_first_of(const E *s, size_type pos = 0) const
size_type find_first_of(E c, size_type pos = 0) const
size_type find_last_of(const basic_string& str,size_type pos = npos) const
size_type find_last_of(const E *s, size_type pos,size_type n = npos) const
size_type find_last_of(const E *s, size_type pos = npos) const
size_type find_last_of(E c, size_type pos = npos) const
size_type find_first_not_of(const basic_string& str,size_type pos = 0) const
size_type find_first_not_of(const E *s, size_type pos,size_type n) const
size_type find_first_not_of(const E *s, size_type pos = 0) const
size_type find_first_not_of(E c, size_type pos = 0) const
size_type find_last_not_of(const basic_string& str,size_type pos = npos) const
size_type find_last_not_of(const E *s, size_type pos,size_type n) 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
迭代器
正向迭代器
-
- iterator begin()
- const_iterator begin() const
- iterator end()
- const_iterator end() const
返回配置器
-
- A get_allocator() const
逆向迭代器
-
- reverse_iterator rbegin()
- const_reverse_iterator rbegin() const
- reverse_iterator rend()
- const_reverse_iterator rend() const
npos
string::npos string::npos的类型是string::size_type,所以,一旦需要把一个索引与npos相比,这个索引值必须是string::size_type类型的.是类的静态成员,更多的情况下,我们可以直接把函数和npos进行比较(如:if(s.find("jia")== string::npos))
注意
- C++字符串并不以’\0’结尾。我的建议是在程序中能使用C++字符串就使用,除非万不得已不选用c_string
- 还有必要再重复一下C++字符串和C字符串转换的问题,许多人会遇到这样的问题,自己做的程序要调用别人的函数、类什么的(比如数据库连接函数Connect(char*,char*)),但别人的函数参 数用的是char*形式的,而我们知道,c_str()、data()返回的字符数组由该字符串拥有,所以是一种const char*,要想作为上面提及的函数的参数,还必须拷贝到一个char*,而我们的原则是能不使用C字符串就不使用。那么,这时候我们的处理方式是:如果 此函数对参数(也就是char*)的内容不修改的话,我们可以这样Connect((char*)UserID.c_str(), (char*)PassWD.c_str()),但是这时候是存在危险的,因为这样转换后的字符串其实是可以修改的(有兴趣地可以自己试一试),所以我强调除非函数调用的时候不对参数进行修改,否则必须拷贝到一个char*上去。当然,更稳妥的办法是无论什么情况都拷贝到一个char*上去。同时我们也祈 祷现在仍然使用C字符串进行编程的高手们写的函数都比较规范,那样 我们就不必进行强制转换了
- 我们可以使用下标操作符[]和函数at()对元素包含的字符进行访问。但是应该注意的是操作符[]并不检查索引是否有效(有效索引0~str.length()),如果索引失效,会引起未定义的行为。而at()会检查,如果使用 at()的时候索引无效,会抛出out_of_range异常
..............................................................................................................................................................
const string Cstr("const string");
string Str("string");
Str[3]; //ok
Str.at(3); //ok
Str[100]; //未定义的行为
Str.at(100); //throw out_of_range
Str[Str.length()] //未定义行为
Cstr[Cstr.length()] //返回 ‘\0’
Str.at(Str.length());//throw out_of_range
Cstr.at(Cstr.length()) throw out_of_range