算法之旅,直奔<algorithm>之十七 find_first_of
来源:互联网 发布:密码破译软件 编辑:程序博客网 时间:2024/06/06 01:17
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::tolowerbool 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;}
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;}
#include <iostream> // std::cout#include <algorithm> // std::find_first_of#include <vector> // std::vector#include <cctype> // std::tolowerbool 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;}
0 0
- 算法之旅,直奔<algorithm>之十七 find_first_of
- 算法之旅,直奔<algorithm>之一 all_of
- 算法之旅,直奔<algorithm>之二 adjacent_find
- 算法之旅,直奔<algorithm>之三 any_of
- 算法之旅,直奔<algorithm>之四 binary_search
- 算法之旅,直奔<algorithm>之五 copy
- 算法之旅,直奔<algorithm>之六 copy_backward
- 算法之旅,直奔<algorithm>之七 copy_if
- 算法之旅,直奔<algorithm>之八 copy_n
- 算法之旅,直奔<algorithm>之九 count
- 算法之旅,直奔<algorithm>之十 count_if
- 算法之旅,直奔<algorithm>之十一 equal
- 算法之旅,直奔<algorithm>之十二 equal_range
- 算法之旅,直奔<algorithm>之十三 fill
- 算法之旅,直奔<algorithm>之十四 fill_n
- 算法之旅,直奔<algorithm>之十五 find
- 算法之旅,直奔<algorithm>之十六 find_end
- 算法之旅,直奔<algorithm>之十八 for_each
- STM32 Internal Flash DFU芯片内部flash代码升级
- jsp页面中select下拉控件设置默认值。
- Cocos2d-x 初级概览
- STM32 USB DFU固件升级必读
- C语言单向循环链表解决约瑟夫问题
- 算法之旅,直奔<algorithm>之十七 find_first_of
- 字节序问题--大端法小端法
- Using Putty on Windows to login Linux securely via OpenSSH
- STM32 USB DFU设备固件升级 工程讲解
- STM32 USB 使用芯片内部flash模拟U盘
- Android学习笔记:Service的调用与音乐播放
- DIY一块DS3231的高精度时钟模块,让时钟DS3231/DS1302自动识别
- DMA学习资料
- The OS for Car