7.查找
(1)find
查找指定子串(或字符c)在母串中出现的位置,从母串的pos位置开始找起,带参数n的版本,只需匹配n个字母即算查找成功:
size_t find_first_of(const string& str, size_t pos = 0) const;
size_t find_first_of(const char* s, size_t pos = 0) const;
size_t find_first_of(const char* s, size_t pos, size_t n) const;
size_t find_first_of(char c, size_t pos = 0) const;
返回值是子串在母串出现时,第一个字母的位置,如查不到,返回string::npos
【课堂作业】:实现子串替换母串的replace_by_str算法
结合find和replace函数,实现将母串中指定子串替换为新的子串的算法,比如有母串“An orange is orange”,调用后将其中第一个“orange”替换为“Apple”。
利用“起始位置(pos)”这个参数,可以方便、高效地实现在母串中重复出现的子串的所有位置。不过如果要找的是母串最后一次出现子串的位置,就可以使用rfind
(2)rfind
size_t rfind(const string& str, size_t pos = npos) const;
size_t rfind(const char* s, size_t pos = npos) const;
size_t rfind(const char* s, size_t pos, size_t n) const;
size_t rfind(char c, size_t pos = npos) const;
起始位置npos默认值都变成代表字符串非法位置的npos,所以容易理解,rfind将只在母串的[0, pos)区间查找子串。但是,返回值仍然是匹配子串(从左到右)第1个字符的位置。
(3)find_first_of / find_first_not_of
刚说完“查找最后一次”,再一看“find_first_of”还以为是查找母串第一处出现的子串位置呢。
错,这里是要查找母串中第一处出现子串中任意一个字符的位置。
size_t find_first_of(const string& str, size_t pos = 0) const;
size_t find_first_of(const char* s, size_t pos = 0) const;
size_t find_first_of(const char* s, size_t pos, size_t n) const;
size_t find_first_of(char c, size_t pos = 0) const;
示例一:从字符串ms中找出,字符串ss中的任意一个字符,第一次出现的位置
实例二:将一个字符串中所含有的元音字母,都替换为*号,例子来自http://www.cplusplus.com。这个网站有很多例子。
反义版本find_first_not_of
在母串中查找第一个在子串中不存在的字符的出现位置:
无论是find_first_of还是find_first_not_of 版本,都同样提供第二个参数pos用于指定从母串中的哪一个位置开始向后找起。
(4)find_last_of / find_last_not_of
find_last_of查找母串中最后一处出现子串中任意一个字符的位置。
find_last_not_of查找母串中最后一处出现未包含在子串中的任意一个字符的位置。
8.比较大小
<, <=, >, >=, == 都可以用在std::string身上,但它们返回的信息只有真或假。
std::string提供了compare成员函数,用于和另一个字符串(包括C风格)作比较,
返回值信息为:
正数:表示本串大于入参
0:表示二者相等
负数:表示本串小于入参
int compare(const string& str) const;
int compare(const char* s) const;
int compare(size_t pos, size_t len
, const string& str) const;
int compare(size_t pos, size_t len
, const char* s) const;
int compare(size_t pos, size_t len, const string& str
, size_t subpos, size_t sublen) const;
int compare(size_t pos, size_t len
, const char* s, size_t n) const;
比较双方可以派出全部字母出场,也可以各自只出部分出场。
其中pos和len限定本串的范围;
subpos和sublen或者n,则限定参比串的范围。
【小提示】:字符串比大小的原则
从第0字符开始比较,碰到一个不同的字符时,比出胜负。
英文字符一句在ASCII表中的次序比较,靠前的较小,如果前面字符都一致,但一方还有未出场的队员,则该方为大。
如果使用std::wstring比较汉字,则依据Unicode值比较,排序意义不大。如果要按汉字的拼音甚至笔划排序,效率好的需要操作系统特定API支持,或者将Unicode转换为GBK编码比较勉强有些效果(在Windows下,可将源代码设置为“系统默认”,然后使用std::string存储并比较,有相同效果)。
另外,compare的返回值,仅可作大于零,等于零,小于零判断其意义,不能依赖于其具体的返回的大小,比如在某环境下,返回4785,在另一环境下,可能只返回1。