1. adjacent_find :查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器,否则返回end().
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 the pred version
return first;
else { ++first; ++next; }
}
}
return last;
}
eg: int array[] = { 1, 2, 3, 4, 5, 6, 7, 7 ,8 };
vector<int> ivec(array, array+sizeof(array));
vector<int>::iterator it = adjacent_find( ivec.begin(), ivec.end() );
if ( it != ivec.end() )
cout << "the first consecutive repeated elements are:" << *it << endl;
2.upper_bond :返回一个迭代器, 指向在有序序列范围插入指定值而不破坏容器顺序的第一个位置
template <class ForwardIterator, class T>
ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (!(value<*it)) // or: if (!comp(value,*it)), for the comp version
{ first=++it; count-=step+1; }
else count=step;
}
return first;
}
3.lower_bond: 返回一个 ForwardIterator ,指向在有序序列范围内的可以插入指定值而不破坏容器顺序的第一个位置
template <class ForwardIterator, class T>
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (*it<value) // or: if (comp(*it,value)), for the comp version
{ first=++it;
count-=step+1; }
else count=step;
}
return first;
}
4. equal_range:
template <class ForwardIterator, class T>
pair<ForwardIterator,ForwardIterator>
equal_range ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it = lower_bound (first,last,value);
return make_pair ( it, upper_bound(it,last,value) );
}
5.
binary_search:
在有序序列中查找const类型的变量
value或者常量
,找到返回
true
。
template <class ForwardIterator, class T>
bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value )
{
first = lower_bound(first,last,value);
return (first!=last && !(value<*first));
}
eg:
//利用 vector<int> ivec;
//binary_search 要求在有序的序列中查找,先排序!
sort( ivec.begin(), ivec.end() );
cout << " Looking for the number 23........" << endl;
if( binary_search( ivec.begin(), ivec.end(), 23 ) //此处的数字“23”,只能写成23或者定义一个const类型的变量!!!!
cout << "Yes , find the number !" << endl;
else
cout << " Sorry, not find the number ! " << endl;
6. count : 统计容器中const类型变量的个数
template <class InputIterator, class T>
ptrdiff_t count ( InputIterator first, InputIterator last, const T& value )
{
ptrdiff_t ret=0;
while (first != last) if (*first++ == value) ++ret;
return ret;
}
7. count_if:: pred 作为count的条件
template <class InputIterator, class Predicate>
ptrdiff_t count_if ( InputIterator first, InputIterator last, Predicate pred )
{
ptrdiff_t ret=0;
while (first != last) if (pred(*first++)) ++ret;
return ret;
}
8.find : 查找容器中const类型变量或者常量在容器中的迭代器, 如果没有的话返回end(), 所以在判断的时候先判断 iterator != *.end()
template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}
9.find_if: find 的升级版 与count和count_if 的关系相似
template<class InputIterator, class Predicate>
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
{
for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
return first;
}
10. find_end
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 the pred version
++it1; ++it2;
if (it2==last2) { ret=first1; break; }
if (it1==last1) return ret;
}
++first1;
}
return ret;
}
11. find_first_if
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;
}
12.search
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 the pred version
++it1; ++it2;
if (it2==last2) return first1;
if (it1==last1) return last1;
}
++first1;
}
return last1;
}
13.search_n
template<class ForwardIterator, class Size, class T>
ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
Size count, const T& value )
{
ForwardIterator it, limit;
Size i;
limit=first; advance(limit,distance(first,last)-count);
while (first!=limit)
{
it = first; i=0;
while (*it==value) // or: while (pred(*it,value)) for the pred version
{ ++it; if (++i==count) return first; }
++first;
}
return last;
}