不会改变操作对象内容的STL算法(元素查找,序列查找,统计元素个数,比较区间,找最值)
2016-01-10 19:51
387 查看
元素查找,序列查找,统计元素个数,比较区间,找最值
一、单个元素查找: find, find_if, adjacent_find, find_first_of
1、find:查找元素val
template<class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val) { while (first!=last) { if (*first==val) return first; ++first; } return last; }
2、find_if:查找满足pred(*first)条件的元素
template<class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred) { while (first!=last) { if (pred(*first)) return first; ++first; } return last; }此时可能的UnaryPredicate有,bind2nd(equal_to<int>(), 0),bind2nd(less<int>(), 0),自定义等
3、adjacent_find:查找两个相邻的元素或者前后两个元素满足条件pred(*first,*next)
template <class ForwardIterator> ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last) { if (first != last) { ForwardIterator next=first; ++next; while (next != last) { if (*first == *next) // or: if (pred(*first,*next)), for version (2) return first; ++first; ++next; } } return last; }
4、find_first_of:查找[first1,last1)内第一个等于[first2,last2)内的任意一个元素,或满足条件pred(*it,*first)的元素
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; }
二、子序列匹配(子序列查找): search, find_end, search_n
1、search 在区间[first1,last1)查找第一次出现的子序列[first2,last2)
Searches the range [first1,last1) for the first occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) { if (first2==last2) return first1; // specified in C++11 while (first1!=last1) { ForwardIterator1 it1 = first1; ForwardIterator2 it2 = first2; while (*it1==*it2) { // or: while (pred(*it1,*it2)) for version 2 ++it1; ++it2; if (it2==last2) return first1; if (it1==last1) return last1; } ++first1; } return last1; }
2、find_end 在区间[first1,last1)查找最后一次出现的子序列[first2,last2)
这个函数名字有点问题,不应该和find归在一起,应该是search_endSearches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) { if (first2==last2) return last1; // specified in C++11 ForwardIterator1 ret = last1; while (first1!=last1) { ForwardIterator1 it1 = first1; ForwardIterator2 it2 = first2; while (*it1==*it2) { // or: while (pred(*it1,*it2)) for version (2) ++it1; ++it2; if (it2==last2) { ret=first1; break; } if (it1==last1) return ret; } ++first1; } return ret; }
3、search_n 查找区间[first,last)中由count个相邻元素形成的子序列,其中所有元素都等于value
Searches the range [first,last) for a sequence of count elements, each comparing equal to val (or for which pred returns true).template<class ForwardIterator, class Size, class T> ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& val) { ForwardIterator it, limit; Size i; limit=first; std::advance(limit,std::distance(first,last)-count); while (first!=limit) { it = first; i=0; while (*it==val) // or: while (pred(*it,val)) for the pred version { ++it; if (++i==count) return first; } ++first; } return last; }
三、计算区间中某个元素个数:count, count_if
1、count:计算区间[first,last)中和val相等的元素的个数
template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val) { typename iterator_traits<InputIterator>::difference_type ret = 0; while (first!=last) { if (*first == val) ++ret; ++first; } return ret; }
2、count_if:计算区间[first,last)中满足条件pred(*first)的元素的个数
template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, UnaryPredicate pred) { typename iterator_traits<InputIterator>::difference_type ret = 0; while (first!=last) { if (pred(*first)) ++ret; ++first; } return ret; }
此时可能的UnaryPredicate有,bind2nd(equal_to<int>(), 0),bind2nd(less<int>(), 0),自定义等
四、for_each遍历区间[first,last)中的每个元素并将Function fn套用在每个元素上
template<class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function fn) { while (first!=last) { fn (*first); ++first; } return fn; // or, since C++11: return move(fn); }
五:比较两个区间:equal, mismatch, lexicographical_compare
1、equal:当区间[first1,last1)中的元素和区间[first2,first2+(last1-first1))一一比较,如果都相等或满足pred(*first1,*first2)条件,则返回true,否则返回false
template <class InputIterator1, class InputIterator2> bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 ) { while (first1!=last1) { if (!(*first1 == *first2)) // or: if (!pred(*first1,*first2)), for version 2 return false; ++first1; ++first2; } return true; }
2、mismatch:返回区间[first1,last1)中的元素和区间[first2,first2+(last1-first1))之间第一个元素值不同的位置(有两个位置,每个区间一个)
template <class InputIterator1, class InputIterator2> pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 ) { while ( (first1!=last1) && (*first1==*first2) ) // or: pred(*first1,*first2), for version 2 { ++first1; ++first2; } return std::make_pair(first1,first2); }
3、lexicographical_compare:以字典排序法比较,[first1,last1)小于[first2,last2)的话,返回true,否则返回false
template <class InputIterator1, class InputIterator2> bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); template <class InputIterator1, class InputIterator2, class Compare> bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); template <class InputIterator1, class InputIterator2> bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) { while (first1!=last1) { if (first2==last2 || *first2<*first1) return false; else if (*first1<*first2) return true; ++first1; ++first2; } return (first2!=last2); }
六:最大值和最小值:min, max, min_element, max_element
1、min:返回两个对象a, b之间比较小的那个或满足!comp(b,a)?a:b;
template <class T> const T& min (const T& a, const T& b) { return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for version (2) }
2、max:返回两个对象a, b之间比较大的那个或满足!comp(b,a)?a:b;
template <class T> const T& max (const T& a, const T& b) { return (a<b)?b:a; // or: return comp(a,b)?b:a; for version (2) }
3、min_element:找到区间[first,last)内最小的元素
template <class ForwardIterator> ForwardIterator min_element ( ForwardIterator first, ForwardIterator last ) { if (first==last) return last; ForwardIterator smallest = first; while (++first!=last) if (*first<*smallest) // or: if (comp(*first,*smallest)) for version (2) smallest=first; return smallest; }
4、max_element:找到区间[first,last)内最大的元素
template <class ForwardIterator> ForwardIterator max_element ( ForwardIterator first, ForwardIterator last ) { if (first==last) return last; ForwardIterator largest = first; while (++first!=last) if (*largest<*first) // or: if (comp(*largest,*first)) for version (2) largest=first; return largest; }
相关文章推荐
- 实践干货!猿题库 iOS 客户端架构设计
- 硬盘的读写原理
- vim中的复制粘贴问题
- [转载]使用兼容ie6 ie7 ie8 FF的text-overflow:ellips
- 关于数组IF({1,0}……结构的含义
- vncserver installation
- 在ESXi主机上关闭无响应的虚拟机
- angularJS任务列表例子
- List 和 map 的赋值方式
- 汇编语言第一个程序
- Linux平台设备驱动 - 按键设备驱动
- 29. Divide Two Integers
- iOS 开发的技巧
- 第8课:彻底实战详解使用IDE开发Spark程序
- LeetCode--word Pattern
- J2EE之13中规范初了解
- struts2环境配置以及配置xml文件智能提示
- C语言数据结构——队列
- Java高效并发
- Linux最小化安装,忘记安装开发工具的解决方法