算法之旅,直奔<algorithm>之十七 find_first_of
2013-12-20 10:31
375 查看
find_first_of(vs2010)
引言这是我学习总结 <algorithm>的第十七篇,find_first_of是匹配的一个函数。<algorithm>是c++的一个头文件的名字,里面集成了好多好多的函数。故取之共享于大家,方便大家了解。
作用
find_first_of 的作用是拿指定数据在原数据中去匹配,返回匹配数据在原数据中的首位置。
原型
template<class InputIterator, class ForwardIterator> InputIterator find_first_of ( InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2) { while (first1!=last1) { for (ForwardIterator it=first2; it!=last2; ++it) { if (*it==*first1) // or: if (pred(*it,*first)) for version (2) return first1; } ++first1; } return last1; }
实验
原数据如下
匹配数据
返回第一个‘A’的位置。
代码
test.cpp
#include <iostream> // std::cout #include <algorithm> // std::find_first_of #include <vector> // std::vector #include <cctype> // std::tolower bool comp_case_insensitive (char c1, char c2) { return (std::tolower(c1)==std::tolower(c2)); } int main () { int mychars[] = {'a','b','c','A','B','C'}; std::vector<char> haystack ( mychars,mychars+6 ); std::vector<char>::iterator it; int needle[] = {'A','B','C'}; // using default comparison: it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3); if (it!=haystack.end()) std::cout << "The first match is: " << *it << '\n'; // using predicate comparison: it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3, comp_case_insensitive); if (it!=haystack.end()) std::cout << "The first match is: " << *it << '\n'; system("pause"); return 0; }
相关文章推荐
- django:Many-to-many关系
- 回顾Google这一年——硬件篇
- GO语言介绍及视频教程
- Real-Time Rendering (7) -加速算法(Acceleration Algorithm)
- Real-Rime Rendering (7) -加速算法(Acceleration Algorithm)
- [LeetCode] Generate Parentheses
- 算法之旅,直奔<algorithm>之十七 find_first_of
- 快速排序----Go语言版
- Manacher’s algorithm(最长回文子串)
- Cargo Daemon远程启动、停止服务器、部署项目
- Cargo Daemon远程启动、停止服务器、部署项目
- HDU Monkey King(左偏树)
- 如何修复google自动跳转google.hk?
- google单元测试框架
- Google API V2申请及Google Map简单应用例子
- [LeetCode] Rotate List
- 深入了解Go Playground
- The Most Important Algorithms
- google code .netrc 在windows中的操作 _netrc
- Google Browser Security Handbook