设为首页 加入收藏

TOP

C++String中用于查找的find系列函数浅析(二)
2015-07-16 12:56:56 来源: 作者: 【 】 浏览:8
Tags:String 用于 查找 find 系列 函数 浅析
s *n th*s s*nt*nc* by *st*r*sks


4.find_last_of()


原型:


//string (1)
size_type find_last_of (const basic_string& str, size_type pos = npos) const noexcept;
//c-string (2)
size_type find_last_of (const charT* s, size_type pos = npos) const;
//buffer (3)
size_type find_last_of (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find_last_of (charT c, size_type pos = npos) const noexcept;


?


说明:
? ? 该函数与find_first_of()函数相似,只不过查找顺序是从指定位置向前,这里仅简单举例,不再赘述,读者可参考find_first_of()自行学习。


示例:


#include
#include


using namespace std;


int main()
{
? ? //测试size_type find_last_of (const charT* s, size_type pos = npos) const;
? ? //目标串中仅有字符c与源串中的两个c匹配,其余字符均不匹配
? ? string str("abcdecg");
? ? cout << str.find_last_of("hjlywkcipn", 6) << endl;//5? 从str的位置6(g)开始想前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5
? ? cout << str.find_last_of("hjlywkcipn", 4) << endl;//2? 从str的位置4(e)开始想前找,e不匹配,再找d,d不匹配,再找c,c匹配,停止查找,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //? ? 返回c在str中的位置5
? ? cout << str.find_last_of("hjlywkcipn", 200) << endl;//5? 当第2个参数超出源串的长度(这里str长度是7)时,不会出错,相当于从源串的最后一
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //? ? 个字符起开始查找
? ? return 0;
}


5.find_first_not_of()
原型:


//string (1)
size_type find_first_not_of (const basic_string& str, size_type pos = 0) const noexcept;
//c-string (2)
size_type find_first_not_of (const charT* s, size_type pos = 0) const;
//buffer (3)
size_type find_first_not_of (const charT* s, size_type pos, size_type n) const;
//character(4)
size_type find_first_not_of (charT c, size_type pos = 0) const noexcept;


说明:
? ? ? 在源串中从位置pos开始往后查找,只要在源串遇到一个字符,该字符与目标串中的任意一个字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满? 足条件的字符,则返回npos。


示例(仅简单举例,有了前边的学习,相信读者可以???己学习find_first_not_of()):


#include
#include


using namespace std;


int main()
{
? ? //测试size_type find_first_not_of (const charT* s, size_type pos = 0) const;
? ? string str("abcdefg");
? ? cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3? 从源串str的位置0(a)开始查找,目标串中有a(匹配),再找b,b匹配,再找c,c匹配,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //? ? 再找d,目标串中没有d(不匹配),停止查找,返回d在str中的位置3
? ? return 0;
}


将前面应用举例中的fing_first_of()换成find_first_not_of(),就可以将字符串中所有非元音字母换成*。请读者自己验证。


6.find_last_not_of()


原型:



//string (1)
size_type find_last_not_of (const basic_string& str, size_type pos = npos) const noexcept;
//c-string (2)
size_type find_last_not_of (const charT* s, size_type pos = npos) const;
//buffer (3)
size_type find_last_not_of (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find_last_not_of (charT c, size_type pos = npos) const noexcept;


说明:
? ? find_last_not_of()与find_first_not_of()相似,只不过查找顺序是从指定位置向前,这里不再赘述,相信读者可以自行学习。


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇jQuery中容易让人困惑的东西 下一篇C++字符串与转义字符

评论

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