C++

C++ std::string使用总结

2018-12-31  本文已影响35人  rmrfany

构造函数(Constructors)

语法:

string();
string( size_type length, char ch );
string( const char *str );
string( const char *str, size_type length );
string( string &str, size_type index, size_type length );
string( input_iterator start, input_iterator end );

字符串的构造函数创建一个新字符串,包括:

添加文本(append)

语法:

basic_string &append( const basic_string &str );
basic_string &append( const char *str );
basic_string &append( const basic_string &str, size_type index, size_type len );
basic_string &append( const char *str, size_type num );
basic_string &append( size_type num, char ch );
basic_string &append( input_iterator start, input_iterator end );

append() 函数可以完成以下工作:

赋值(assign)

语法:

basic_string &assign( const basic_string &str );
basic_string &assign( const char *str );
basic_string &assign( const char *str, size_type num );
basic_string &assign( const basic_string &str, size_type index, size_type len );
basic_string &assign( size_type num, char ch );

函数以下列方式赋值:

at

语法:

reference at( size_type index );

at()函数返回一个引用,指向在index位置的字符. 如果index不在字符串范围内, at() 将报告"out of range"错误,并抛出out_of_range异常。

begin

语法:

iterator begin();

begin()函数返回一个迭代器,指向字符串的第一个元素。

c_str

语法:

const char *c_str();

c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同.

容量(capacity)

语法:

size_type capacity();

capacity()函数返回在重新申请更多的空间前字符串可以容纳的字符数. 这个数字至少与 size()一样大.

比较(compare)

语法:

int compare( const basic_string &str );
int compare( const char *str );
int compare( size_type index, size_type length, const basic_string &str );
int compare( size_type index, size_type length, const basic_string &str, size_type index2,
size_type length2 );
int compare( size_type index, size_type length, const char *str, size_type length2 );

compare()函数以多种方式比较本字符串和str,返回:
返回值情况:
小于零 this < str
零 this == str
大于零 this > str

拷贝(copy)

语法:

size_type copy( char *str, size_type num, size_type index );

copy()函数拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数

data

语法:

const char *data();

data()函数返回指向自己的第一个字符的指针.

empty

语法:

bool empty();

如果字符串为空则empty()返回真(true),否则返回假(false).

end

语法:

iterator end();

end()函数返回一个迭代器,指向字符串的末尾(最后一个字符的下一个位置).

删除(erase)

语法:

iterator erase( iterator pos );
iterator erase( iterator start, iterator end );
basic_string &erase( size_type index = 0, size_type num = npos );

erase()函数可以:

参数index num 有默认值, 这意味着erase()可以这样调用:只带有index以删除index后的所有字符,或者不带有任何参数以删除所有字符.

查找(find)

语法:

size_type find( const basic_string &str, size_type index );
size_type find( const char *str, size_type index );
size_type find( const char *str, size_type index, size_type length );
size_type find( char ch, size_type index );

find()函数:

find_first_of

语法:

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_of()函数:

find_first_not_of

语法:

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_first_not_of()函数:

find_last_of

语法:

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_of()函数:

find_last_not_of

语法:

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 );

find_last_not_of()函数:

get_allocator

语法:

allocator_type get_allocator();

get_allocator()函数返回本字符串的配置器.

插入(insert)

语法:

iterator insert( iterator i, const char &ch );
basic_string &insert( size_type index, const basic_string &str );
basic_string &insert( size_type index, const char *str );
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
basic_string &insert( size_type index, const char *str, size_type num );
basic_string &insert( size_type index, size_type num, char ch );
void insert( iterator i, size_type num, const char &ch );
void insert( iterator i, iterator start, iterator end );

insert()函数的功能非常多:

长度(length)

语法:

size_type length();

length()函数返回字符串的长度. 这个数字应该和size()返回的数字相同.

max_size
语法:

size_type max_size();

max_size()函数返回字符串能保存的最大字符数。

rbegin

语法:

const reverse_iterator rbegin();

rbegin()返回一个逆向迭代器,指向字符串的最后一个字符。

rend

语法:

const reverse_iterator rend();

rend()函数返回一个逆向迭代器,指向字符串的开头(第一个字符的前一个位置)。

替换(replace)

语法:

basic_string &replace( size_type index, size_type num, const basic_string &str );
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
size_type num2 );
basic_string &replace( size_type index, size_type num, const char *str );
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
basic_string &replace( iterator start, iterator end, const basic_string &str );
basic_string &replace( iterator start, iterator end, const char *str );
basic_string &replace( iterator start, iterator end, const char *str, size_type num );
basic_string &replace( iterator start, iterator end, size_type num, char ch );

replace()函数:

保留空间(reserve)

语法:

void reserve( size_type num );

reserve()函数设置本字符串的capacity 以保留num个字符空间。

resize

语法:

void resize( size_type num );
void resize( size_type num, char ch );

resize()函数改变本字符串的大小到num, 新空间的内容不确定。也可以指定用ch填充。

rfind

语法:

size_type rfind( const basic_string &str, size_type index );
size_type rfind( const char *str, size_type index );
size_type rfind( const char *str, size_type index, size_type num );
size_type rfind( char ch, size_type index );

rfind()函数:

size

语法:

size_type size();

size()函数返回字符串中现在拥有的字符数。

substr

语法:

basic_string substr( size_type index, size_type num = npos );

substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。

交换(swap)

语法:

void swap( basic_string &str );

swap()函数把str和本字符串交换。

上一篇 下一篇

猜你喜欢

热点阅读