探究string类
来源:互联网 发布:我是歌手 知乎 编辑:程序博客网 时间:2024/06/04 23:26
string是非常强大的类型,很好的封装了字符串的操作
其实,string并不是一个单独的容器,只是basic_string 模板类的一个typedef 而已,相对应的还有wstring, 你在string 头文件中你会发现下面的代码:
extern "C++" {typedef basic_string <char> string;typedef basic_string <wchar_t> wstring;} // extern "C++"
由于只是解释string的用法,如果没有特殊的说明,本文并不区分string 和 basic_string的区别。
string 其实相当于一个保存字符的序列容器,因此除了有字符串的一些常用操作以外,还有包含了所有的序列容器的操作。字符串的常用操作包括:增加、删除、修改、查找比较、链接、输入、输出等。详细函数列表参看附录。不要害怕这么多函数,其实有许多是序列容器带有的,平时不一定用的上。
如果你要想了解所有函数的详细用法,你需要查看basic_string,或者下载STL编程手册。这里通过实例介绍一些常用函数:
string操作如下:构造函数:
string();//空串string( size_type length, char ch );//以length为长度的ch的拷贝(即length个ch)
string( const char *str );//以str为初值 (长度任意)
string( const char *str, size_type length );//同上,长度不限,但注意不要越界,以免发生不可预知问题
string( string &str, size_type index, size_type length );//以index为索引开始的子串,长度为length, 或者小于length
string( input_iterator start, input_iterator end );//以从start到end的元素为初值
支持的操作符:
==>
<
>=
<=
!=
+
+=
[]
追加文本(append)
basic_string &append( const basic_string &str );//在字符串的末尾添加strbasic_string &append( const char *str );//在字符串末尾添加str所指向的c风格字符串
basic_string &append( const basic_string &str, size_type index, size_type len );//在字符串的末尾添加str的子串,子串以index索引开始,长度为len
basic_string &append( const char *str, size_type num );//在字符串的末尾添加str中的num个字符
basic_string &append( size_type num, char ch );//在字符串的末尾添加num个字符ch
basic_string &append( input_iterator start, input_iterator end );//在字符串的末尾添加以迭代器start和end表示的字符序列
赋值(assign)
basic_string &assign( const basic_string &str );//用str为字符串赋值basic_string &assign( const char *str );//用str c风格为字符串赋值
basic_string &assign( const char *str, size_type num );//用str的开始num个字符为字符串赋值
basic_string &assign( const basic_string &str, size_type index, size_type len );//用str的子串为字符串赋值,子串以index索引开始,长度为len
basic_string &assign( size_type num, char ch );//用num个字符ch为字符串赋值
比较(compare)
int compare( const basic_string &str );//比较自己和strint compare( const char *str );//比较自己和str
int compare( size_type index, size_type length, const basic_string &str );//比较自己的子串和str,子串以index索引开始,长度为length
int compare( size_type index, size_type length, const basic_string &str, size_type index2,
size_type length2 );//比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
int compare( size_type index, size_type length, const char *str, size_type length2 );//比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length
返回值 情况
小于零 this < str
零 this == str
大于零 this > str
删除(erase)
iterator erase( iterator pos );//删除pos指向的字符, 返回指向下一个字符的迭代器iterator erase( iterator start, iterator end );//删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置
basic_string &erase( size_type index = 0, size_type num = npos );//删除从index索引开始的num个字符, 返回*this
插入(insert)
iterator insert( iterator i, const char &ch );//在迭代器i表示的位置前面插入一个字符chbasic_string &insert( size_type index, const basic_string &str );//在字符串的位置index插入字符串str
basic_string &insert( size_type index, const char *str );//在字符串的位置index插入字符串str
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );//在字符串的位置index插入字符串str的子串(从index2开始,长num个字符)
basic_string &insert( size_type index, const char *str, size_type num );//在字符串的位置index插入字符串str的num个字符
basic_string &insert( size_type index, size_type num, char ch );//在字符串的位置index插入num个字符ch的拷贝
void insert( iterator i, size_type num, const char &ch );//在迭代器i表示的位置前面插入num个字符ch的拷贝
void insert( iterator i, iterator start, iterator end );//在迭代器i表示的位置前面插入一段字符,从start开始,以end结束
替换(replace)
basic_string &replace( size_type index, size_type num, const basic_string &str );//用str中的num个字符替换本字符串中的字符,从index开始basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
size_type num2 );//用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
basic_string &replace( size_type index, size_type num, const char *str );//用str中的num个字符(从index开始)替换本字符串中的字符
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );//用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );//用num2个ch字符替换本字符串中的字符,从index开始,num1个字符
basic_string &replace( iterator start, iterator end, const basic_string &str );//用str中的字符替换本字符串中的字符,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, const char *str );//用str替换本字符串中的内容,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, const char *str, size_type num );//用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围
basic_string &replace( iterator start, iterator end, size_type num, char ch );//用num个ch字符替换本字符串中的内容,迭代器start和end指示范围
查找
一下各种find函数十分相似,所以就不一一注释了返回值:如果没找到则返回string::npos
find:
size_type find( const basic_string &str, size_type index );//返回str在字符串中第一次出现的位置(从index开始查找)
size_type find( const char *str, size_type index );//返回str在字符串中第一次出现的位置(从index开始查找)
size_type find( const char *str, size_type index, size_type length );//返回str在字符串中第一次出现的位置(从index开始查找,长度为length)
size_type find( char ch, size_type index );//返回字符ch在字符串中第一次出现的位置(从index开始查找)
find_first_of:查找在字符串中第一个与str中的某个字符匹配的字符
size_type find_first_of( const basic_string &str, size_type index = 0 );
size_type find_first_of( const char *str, size_type index = 0 );
size_type find_first_of( const char *str, size_type index, size_type num );
size_type find_first_of( char ch, size_type index = 0 );
find_first_not_of:在字符串中查找第一个与str中的字符都不匹配的字符
size_type find_first_not_of( const basic_string &str, size_type index = 0 );
size_type find_first_not_of( const char *str, size_type index = 0 );
size_type find_first_not_of( const char *str, size_type index, size_type num );
size_type find_first_not_of( char ch, size_type index = 0 );
find_last_of:在字符串中查找最后一个与str中的某个字符匹配的字符
size_type find_last_of( const basic_string &str, size_type index = npos );
size_type find_last_of( const char *str, size_type index = npos );
size_type find_last_of( const char *str, size_type index, size_type num );
size_type find_last_of( char ch, size_type index = npos );
find_last_not_of:在字符串中查找最后一个与str中的字符都不匹配的字符
size_type find_last_not_of( const basic_string &str, size_type index = npos );
size_type find_last_not_of( const char *str, size_type index = npos);
size_type find_last_not_of( const char *str, size_type index, size_type num );
size_type find_last_not_of( char ch, size_type index = npos );
rfind函数
size_type rfind( const basic_string &str, size_type index );//返回最后一个与str中的某个字符匹配的字符,从index开始查找
size_type rfind( const char *str, size_type index );//返回最后一个与str中的某个字符匹配的字符,从index开始查找
size_type rfind( const char *str, size_type index, size_type num );//返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符
size_type rfind( char ch, size_type index );//返回最后一个与ch匹配的字符,从index开始查找
at函数
reference at( size_type index );//at()函数返回一个引用,指向在index位置的字符. 如果index不在字符串范围内, at() 将报告"out of range"错误,并抛出out_of_range异常
begin函数
iterator begin();//begin()函数返回一个迭代器,指向字符串的第一个元素
end函数
iterator end();//返回一个迭代器,指向字符串的末尾(最后一个字符的下一个位置)
c_str函数
const char *c_str();//返回一个指向正规C字符串的指针, 内容与本字符串相同
capacity函数
size_type capacity();//返回在重新申请更多的空间前字符串可以容纳的字符数. 这个数字至少与 size()一样大
copy函数
size_type copy( char *str, size_type num, size_type index );//拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数
data函数
const char *data();//返回指向自己的第一个字符的指针
empty函数
bool empty();//如果字符串为空则empty()返回真(true),否则返回假(false)
get_allocator函数
allocator_type get_allocator();//返回本字符串的配置器
length函数
size_type length();//返回字符串的长度. 这个数字应该和size()返回的数字相同
max_size
size_type max_size();//返回字符串能保存的最大字符数
rbegin函数
rbegin();//返回一个逆向迭代器,指向最后一个字符
rend函数
rend();//返回一个逆向迭代器,指向第一个元素的前一个位置
reserve函数
reserve( size_type num );//保留一定容量以容纳字符串(设置capacity值)
resize函数
void resize( size_type num );//改变本字符串的大小到num, 新空间的内容不确定
void resize( size_type num, char ch );//也可以指定用ch填充
size函数
size();//返回字符串中字符的数量
substr函数
basic_string substr( size_type index, size_type num = npos );//返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串
swap函数
void swap( basic_string &str );//把str和本字符串交换
- 探究string类
- java String 类源代码探究
- JAVA中String类深度探究
- JAVA中String类深度探究
- 探究string的应用
- 探究std::string
- String.intern()探究
- java中String类中split()在探究。【Java】
- 【Java笔记】深入探究String类内存机制
- 探究string结尾有木有'\0'
- Java中String的探究
- 【探究】String.format()的用法
- 关于const、友元函数、string类、类继承等内容的探究
- 关于string类对象不是以空字符标记字符串末尾的探究
- java String常见细节问题探究
- 探究String、StringBuffer、StringBuilder的区别?
- 探究java中的String、StringBuilder以及StringBuffer
- 《深入理解java虚拟机》String.intern()探究
- android欢迎界面的绘制
- Codeforces Round #207 (Div. 2)C. Knight Tournament(SET也可以搞定)
- IOS内存管理总结
- 关于“ARC forbids explicit message send of release”错误
- 企业级敏捷转型系列专题之四 —— 企业转型
- 探究string类
- JDK5.0环境下配置PKCS#11
- HDU-1081 To The Max 二维最大子序列和
- 项目总结
- Python Tips, Tricks, and Idioms and Learn Python Through Public Data Hacking
- stl之map erase方法的正确使用
- 远程桌面教程及指定主机远程连接方法
- 求解器Z3(未完,更新中)
- android 调用系统图库查看指定路径的图片