c++ algorithm库

copy

template<class InputIterator, class OutputIterator>

  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )

{

  while (first!=last) *result++ = *first++;

  return result;

}

std::copy(v_int.begin(),v_int.end(),v_i.begin());         

把v_int容器的内容复制到v_i容器中

std::copy(v_int.begin(), v_int.end(), ostream_iterator(cout, " "));         把v_int容器的内容输出到终端

copy_backward

template<class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,
                                         BidirectionalIterator1 last,
                                         BidirectionalIterator2 result )
{
  while (last!=first) *(--result) = *(--last);
  return result;
}

std::copy_backward(myvector.begin(), myvector.begin()+5, myvector.end() );  把myvector容器的5个元素复制到最后(myvector.resize(myvector.size()+3);)

accumulate

#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int myfunction (int x, int y) {return x+2*y;}
struct myclass {
  int operator()(int x, int y) {return x+3*y;}
} myobject;
int main () {
  int init = 100;
  int numbers[] = {10,20,30};
  cout << "using default accumulate: ";
  cout << accumulate(numbers,numbers+3,init);
  cout << endl;
  cout << "using functional's minus: ";
  cout << accumulate (numbers, numbers+3, init, minus<int>() );
  cout << endl;  
  cout << "using custom function: ";
  cout << accumulate (numbers, numbers+3, init, myfunction );
  cout << endl;
  cout << "using custom object: ";
  cout << accumulate (numbers, numbers+3, init, myobject );
  cout << endl;
  return 0;
}
/*
output:
using default accumulate: 160
using functional's minus: 40
using custom function: 220
using custom object: 280
*/

for_each

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;

}

replace

template < class ForwardIterator, class T >

  void replace ( ForwardIterator first, ForwardIterator last,

                 const T& old_value, const T& new_value )

{

  for (; first != last; ++first)

    if (*first == old_value) *first=new_value;

}

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

 

int main () {

  int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };

  vector<int> myvector (myints, myints+8);            // 10 20 30 30 20 10 10 20

 

  replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 99

 

  cout << "myvector contains:";

  for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)

    cout << " " << *it;

 

  cout << endl;

 

  return 0;

}

remove

从实现来看与erase()不同,通用算法中的remove()也与容器list的成员函数remove()不同,通用算法中的remove()并没有容器中删除东西而是把非指定的值位置提前替换掉指定值占用的位置。

template < class ForwardIterator, class T >

  ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T& value )

{

  ForwardIterator result = first;

  for ( ; first != last; ++first)

    if (!(*first == value)) *result++ = *first;

  return result;

}

// remove algorithm example

#include <iostream>

#include <algorithm>

using namespace std;

 

int main () {

  int myints[] = {10,20,30,30,20,10,10,20};      // 10 20 30 30 20 10 10 20

 

  // bounds of range:

  int* pbegin = myints;                          // ^

  int* pend = myints+sizeof(myints)/sizeof(int); // ^                       ^

 

  pend = remove (pbegin, pend, 20);              // 10 30 30 10 10 ?  ?  ?

                                                 // ^              ^

  cout << "range contains:";

  for (int* p=pbegin; p!=pend; ++p)

    cout << " " << *p;

 

  cout << endl;

 

  return 0;

}

remove_if

应用函数而不具体指定值来判断要删除的元素 remove_if()

template < class ForwardIterator, class Predicate >

  ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,

                              Predicate pred )

{

  ForwardIterator result = first;

  for ( ; first != last; ++first)

    if (!pred(*first)) *result++ = *first;

  return result;

}

// remove_if example

#include <iostream>

#include <algorithm>

using namespace std;

 

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

 

int main () {

  int myints[] = {1,2,3,4,5,6,7,8,9};            // 1 2 3 4 5 6 7 8 9

 

  // bounds of range:

  int* pbegin = myints;                          // ^

  int* pend = myints+sizeof(myints)/sizeof(int); // ^                 ^

 

  pend = remove_if (pbegin, pend, IsOdd);        // 2 4 6 8 ? ? ? ? ?

                                                 // ^       ^

  cout << "range contains:";

  for (int* p=pbegin; p!=pend; ++p)

    cout << " " << *p;

 

  cout << endl;

 

  return 0;

}

count

统计范围内等于指定值的元素个数 count()

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 algorithm example

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

 

int main () {

  int mycount;

 

  // counting elements in array:

  int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements

  mycount = (int) count (myints, myints+8, 10);

  cout << "10 appears " << mycount << " times.\n";

 

  // counting elements in container:

  vector<int> myvector (myints, myints+8);

  mycount = (int) count (myvector.begin(), myvector.end(), 20);

  cout << "20 appears " << mycount  << " times.\n";

 

  return 0;

}

find

查找范围内等于指定值的第一个元素的位置 find()

template<class InputIterator, class T>

  InputIterator find ( InputIterator first, InputIterator last, const T& value )

  {

    for ( ;first!=last; first++) if ( *first==value ) break;

    return first;

  }

Serch

查找字串是否存在,没有就是第二个迭代器

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;

}

// search algorithm example

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

 

bool mypredicate (int i, int j) {

  return (i==j);

}

 

int main () {

  vector<int> myvector;

  vector<int>::iterator it;

 

  // set some values:        myvector: 10 20 30 40 50 60 70 80 90

  for (int i=1; i<10; i++) myvector.push_back(i*10);

 

 

  // using default comparison:

  int match1[] = {40,50,60,70};

  it = search (myvector.begin(), myvector.end(), match1, match1+4);

 

  if (it!=myvector.end())

    cout << "match1 found at position " << int(it-myvector.begin()) << endl;

  else

    cout << "match1 not found" << endl;

 

  // using predicate comparison:

  int match2[] = {20,30,50};

  it = search (myvector.begin(), myvector.end(), match2, match2+3, mypredicate);

 

  if (it!=myvector.end())

    cout << "match2 found at position " << int(it-myvector.begin()) << endl;

  else

    cout << "match2 not found" << endl;

 

  return 0;

}

template <class Arg1, class Arg2, class Result>

  struct binary_function {

    typedef Arg1 first_argument_type;

    typedef Arg2 second_argument_type;

    typedef Result result_type;

  };

template <class T> struct greater : binary_function <T,T,bool> {

  bool operator() (const T& x, const T& y) const

    {return x>y;}

};

// greater example

#include <iostream>

#include <functional>

#include <algorithm>

using namespace std;

 

int main () {

  int numbers[]={20,40,50,10,30};

  sort (numbers, numbers+5, greater<int>() );

  for (int i=0; i<5; i++)

    cout << numbers[i] << " ";

  cout << endl;

  return 0;

}

 

原文地址:https://www.cnblogs.com/ghost240/p/2531487.html