C++的STL之find_first_of
来源:互联网 发布:java面向对象程序设计 编辑:程序博客网 时间:2024/05/20 04:13
find_first_of //找第一个符合条件的位置
find_last_of //找最后一个符合条件的位置
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; /***************************************** //所有容器适用 find_first_of(b,e,sb,se); find_first_of(b,e,sb,se,bp); 使用逆向迭代器 实现find_last_of算法 *****************************************/ /****************************************************** string查找函数和STL查找算法的比较 -------------------------------------------- string函数 STL算法 find() find() rfind() find()+逆向迭代器 find() search() find() find_end() find_first_of() find_first_of() find_last_of() find_first_of()+逆向迭代器 ******************************************************/ /************************************************************************************* std::find_first_of() 所有容器适用 algorithm -------------------------------------------------------------------------------------- template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2 ); template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred ); //eg: template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) { for ( ; first1 != last1; ++first1 ) for (ForwardIterator2 it=first2; it!=last2; ++it) if (*it==*first1) // or: if (comp(*it,*first)) for the pred version return first1; return last1; } *************************************************************************************/ //不分大小写 bool comp_case_insensitive (char c1, char c2) { return (tolower(c1)==tolower(c2)); } void SplitFilename (const string& str); int main () { int mychars[] = {'a','b','c','A','B','C'}; vector<char> myvector (mychars,mychars+6); vector<char>::iterator it; int match[] = {'A','B','C'}; cout<<"母串:a b c A B C\n"; cout<<"子串:A B C\n"; // using default comparison: it = find_first_of (myvector.begin(), myvector.end(), match, match+3); if (it!=myvector.end()) cout << "first match is: " << *it << endl; // using predicate comparison: it = find_first_of (myvector.begin(), myvector.end(), match, match+3, comp_case_insensitive); if (it!=myvector.end()) cout << "first match is: " << *it << endl; /**----------------------------find_first_of()+逆向迭代器-------------------------------**/ vector<char>::reverse_iterator rit; // 逆向迭代器 rit = find_first_of (myvector.rbegin(), myvector.rend(), match, match+3); if (rit!=myvector.rend()) cout << "last match is: " << *rit << endl; cout<<endl; /**----------------------------string-------------------------------**/ /********************************************************************* std::string::find_last_of string ----------------------------------------------------------------------- size_t find_last_of ( const string& str, size_t pos = npos ) const; size_t find_last_of ( const char* s, size_t pos, size_t n ) const; size_t find_last_of ( const char* s, size_t pos = npos ) const; size_t find_last_of ( char c, size_t pos = npos ) const; ***********************************************************************/ string str1 ("/usr/bin/man"); string str2 ("c:\\windows\\winhelp.exe"); SplitFilename (str1); cout<<endl; SplitFilename (str2); return 0; } void SplitFilename (const string& str) { size_t found; cout << "Splitting: " << str << endl; found=str.find_last_of("\\/"); cout << " folder: " << str.substr(0,found) << endl; cout << " file: " << str.substr(found+1) << endl; } /***** Output 母串:a b c A B C 子串:A B C first match is: A first match is: a last match is: C Splitting: /usr/bin/man folder: /usr/bin file: man Splitting: c:\windows\winhelp.exe folder: c:\windows file: winhelp.exe */
0 0
- C++的STL之find_first_of
- STL 之find,find_if,find_end,find_first_of
- STL 之find,find_if,find_end,find_first_of
- STL可能的误用-find_first_of和erase
- 简单的程序诠释C++ STL算法系列之五:find_first_of
- find_first_of (STL Sample)
- STL(5)find_first_of函数
- stl string的substr和find_first_of函数的一点心得
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- STL algorithm算法find_first_of(18)
- c++之STL(13) STL 算法 - 查找算法(4)find_first_of(b,e,sb,se) find_first_of(b,e,sb,se,bp)
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- find_first_of
- find_first_of
- openwrt远程登录总结
- 分析AJAX抓取今日头条街拍美图(上)
- 【C#】sqlitehelper类
- [Leetcode] #234 Palindrome Linked List
- 进程和线程的区别和linux运行状态查看简单介绍
- C++的STL之find_first_of
- 内存对齐———结构体
- 欢迎使用CSDN-markdown编辑器
- servlet request getHeader("x-forwarded-for") 获取真实IP
- C#网络电视精灵系统
- [IOS APP]窥破金瓶梅-西门庆真的很好色吗
- Windows下ActiveMQ下载、安装部署
- 真正从零开始,TensorFlow详细安装入门图文教程!
- 【BZOJ 1864】【ZJOI 2006】三色二叉树【树型DP】