整理下STL algorithms(1)

for_each
Function for_each (InputIterator first, InputIterator last, Function f);
将函数f应用于first和last之间的元素。相当于如下的行为
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for ( ; first!=last; ++first ) f(*first);
return f;
}
可以如下应用:
#include <iostream>
#include
<algorithm>
#include
<vector>
using namespace std;

void myfunction (int i) {
cout
<< " " << i;
}

struct myclass {
void operator() (int i) {cout << " " << i;}
} myobject;

int main () {
vector
<int> myvector;
myvector.push_back(
10);
myvector.push_back(
20);
myvector.push_back(
30);

cout
<< "myvector contains:";
for_each (myvector.begin(), myvector.end(), myfunction);

// or:
cout << "\nmyvector contains:";
for_each (myvector.begin(), myvector.end(), myobject);

cout
<< endl;

return 0;
}

find
InputIterator find ( InputIterator first, InputIterator last, const T& value );
在first和last之间查找元素,返回第一个找到的迭代器,否则,返回last
template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last,
const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}
使用
#include <iostream>
#include
<algorithm>
#include
<vector>
using namespace std;

int main () {
int myints[] = { 10, 20, 30 ,40 };
int * p;

// pointer to array element:
p = find(myints,myints+4,30);
++p;
cout
<< "The element following 30 is " << *p << endl;

vector
<int> myvector (myints,myints+4);
vector
<int>::iterator it;

// iterator to vector element:
it = find (myvector.begin(), myvector.end(), 30);
++it;
cout
<< "The element following 30 is " << *it << endl;

return 0;
}
find_if
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred );
在first 和last之间查找满足pred条件的元素
实例:
#include <iostream>
#include
<algorithm>
#include
<vector>
using namespace std;

bool IsOdd (int i) {
return ((i%2)==1);
}

int main () {
vector
<int> myvector;
vector
<int>::iterator it;

myvector.push_back(
10);
myvector.push_back(
25);
myvector.push_back(
40);
myvector.push_back(
55);

it
= find_if (myvector.begin(), myvector.end(), IsOdd);
cout
<< "The first odd value is " << *it << endl;

return 0;
}
find_end
template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
                               ForwardIterator2 first2, ForwardIterator2 last2 );

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
                               ForwardIterator2 first2, ForwardIterator2 last2.
                               BinaryPredicate pred );
在first1和last1序列中找到最后一个first2和last2之间的序列~~~pred用于定义比较函数~返回的是第一个序列中的相等子序列的起始位置~
#include <iostream>
#include
<algorithm>
#include
<vector>
using namespace std;

bool myfunction (int i, int j) {
return (i==j);
}

int main () {
int myints[] = {1,2,3,4,5,1,2,3,4,5};
vector
<int> myvector (myints,myints+10);
vector
<int>::iterator it;

int match1[] = {1,2,3};

// using default comparison:
it = find_end (myvector.begin(), myvector.end(), match1, match1+3);

if (it!=myvector.end())
cout
<< "match1 last found at position " << int(it-myvector.begin()) << endl;

int match2[] = {4,5,1};

// using predicate comparison:
it = find_end (myvector.begin(), myvector.end(), match2, match2+3, myfunction);

if (it!=myvector.end())
cout
<< "match2 last found at position " << int(it-myvector.begin()) << endl;

return 0;
}
与之想对应的是
find_first_of...这里不再仔细介绍~~~
adjcent_find:
template <class ForwardIterator>
   ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last );

template <class ForwardIterator, class BinaryPredicate>
   ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last,
                                   BinaryPredicate pred );
函数adjacent_find()搜索start和end之间的相邻的两个相同元素. 如果二元谓词pr被指定, 则使用它检查两个元素是否相等. 返回值是一个迭代器, 它指向被发现的元素对的第一个元素. 如果没有匹配的元素, 则返回迭代器end. 
#include <iostream>
#include
<algorithm>
#include
<vector>
using namespace std;

bool myfunction (int i, int j) {
return (i==j);
}

int main () {
int myints[] = {10,20,30,30,20,10,10,20};
vector
<int> myvector (myints,myints+8);
vector
<int>::iterator it;

// using default comparison:
it = adjacent_find (myvector.begin(), myvector.end());

if (it!=myvector.end())
cout
<< "the first consecutive repeated elements are: " << *it << endl;

//using predicate comparison:
it = adjacent_find (++it, myvector.end(), myfunction);

if (it!=myvector.end())
cout
<< "the second consecutive repeated elements are: " << *it << endl;

return 0;
}

count:
 count ( ForwardIterator first, ForwardIterator last, const T& value );
计算first和last之间值为value的元素的数目,相当于如下的实现:
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;
}
相应的是
count_if
count_if ( ForwardIterator first, ForwardIterator last, Predicate pred );
可以指定用于相等的谓词~~
mismatch:
找到两个序列第一个第一个不匹配的元素的迭代器~~返回类型为pair
template <class InputIterator1, class InputIterator2>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2 );

template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2, BinaryPredicate pred );
相当于如下代码:
template <class InputIterator1, class InputIterator2>
pair
<InputIterator1, InputIterator2>
mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
while ( first1!=last1 )
{
if (*first1 != *first2) // or: if (!pred(*first1,*first2)), for pred version
break;
++first1; ++first2;
}
return make_pair(first1,first2);
}
equal:
<algorithm>
template <class InputIterator1, class InputIterator2>
  bool equal ( InputIterator1 first1, InputIterator1 last1,
               InputIterator2 first2 );

template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  bool equal ( InputIterator1 first1, InputIterator1 last1,
               InputIterator2 first2, BinaryPredicate pred );

测试两个序列是否相同
相当于:
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 pred version
return false;
++first1; ++first2;
}
return true;
}
search:
template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
                             ForwardIterator2 first2, ForwardIterator2 last2 );

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
                             ForwardIterator2 first2, ForwardIterator2 last2.
                             BinaryPredicate pred );

查找子序列,跟find_end类似~~
相当于代码:
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
ForwardIterator1 it1, limit;
ForwardIterator2 it2;

limit
=first1; advance(limit,1+distance(first1,last1)-distance(first2,last2));

while (first1!=limit)
{
it1
= first1; it2 = first2;
while (*it1==*it2) // or: while (pred(*it1,*it2)) for the pred version
{ ++it1; ++it2; if (it2==last2) return first1; }
++first1;
}
return last1;
}
search_n:
template <class ForwardIterator, class Size, class T>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& value );

template <class ForwardIterator, class Size, class T, class BinaryPredicate>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& value. BinaryPredicate pred );
search_n是从一个[first, last)范围的数据中查找有count个连续的数据,每个数据都和value相同。它将返回查找到的第一个数据的迭代指针,如果找不到,则返回last。
两个search_n 的不同点在于如何来判断两个数据是相同的:第一个是直接用等于operator==,另一个则需要一个用户定义的函数对象binary_pred。
相当于代码:
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;
}




 
posted @ 2011-04-12 20:19  macula7  阅读(461)  评论(2编辑  收藏  举报