设为首页 加入收藏

TOP

[C++STDLib基础]关于C++string字符串的操作――C++标准库头文件<string> (四)
2015-11-21 01:22:52 来源: 作者: 【 】 浏览:12
Tags:STDLib 基础 关于 string 字符串 操作 标准 文件 < string>
到end的元素为初值.
*/
void test0()
{??
?string str1(5,'c');
?string str2("Now is the time...");
?string str3(str2,5);
?string str4(str2,1,4);
?cout< ?cout< ?cout< ?cout< }
//2.操作符
/*
1.用==, >, <, >=, <=, and !=比较字符串,compare()
2.用 + 或者 += 操作符连接两个字符串
3.用[]获取特定的字符-->at()
A.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 );
返回值 情况 :小于零 this < str ;零 this == str ;大于零 this > str
不同的函数:
1.比较自己和str,
2.比较自己的子串和str,子串以index索引开始,长度为length
3.比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
4.比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length
*/
void test1()
{ ?
?string str1("hello");
?cout< }
//添加文本 append();赋值assign()
/*
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 );
append() 函数可以完成以下工作:
1.在字符串的末尾添加str,
2.在字符串的末尾添加str的子串,子串以index索引开始,长度为len
3.在字符串的末尾添加str中的num个字符,
4.在字符串的末尾添加num个字符ch,
5.在字符串的末尾添加以迭代器start和end表示的字符序列.

B==》与append()完全相同。
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 );
*/
void test2()
{???
?string str1=("hello");
?str1.append(" ");
?char* ch="world";
?str1.append(ch);
?str1.append(3,'!');
?string str2="ni hai hao ma?????";
?str1.append(str2,0,str2.length()-4);
?str1.append("veryGood!!!",5);
?cout<

?string str4, str3 = "War and Peace";
?str4.assign( str3, 4, 3 );?
?cout << str4 << endl;//and
}
//查找find(),find_first_of(), find_first_not_of(),find_last_of(),find_last_not_of(),rfind()
/*
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()函数:
1.返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos,
2.返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
3.返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos

find_first_of()函数:
1.查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
?size_type find_first_of( const char *str, size_type index, size_type num );
2.查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::npos,
3.查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始。

find_first_not_of()函数:
1.在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_type find_first_not_of( const char *str, size_type index, size_type num );
2.在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符。如果没找到就返回string::nops
3.在字符串中查找第一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

find_last_of()函数:
1.在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
?size_type find_last_of( const char *str, size_type index, size_type num );
2.在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::nops
3.在字符串中查找最后一个与ch匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

fin

首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇对A*算法的路径进行优化 下一篇※编程随笔※=>☆编程基础☆=&..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: