# STL中的algorithm

# 1.std::partition函数

template <class ForwardIterator, class UnaryPredicate>  ForwardIterator partition (ForwardIterator first, ForwardIterator last, UnaryPredicate pred);

重新组织容器[first,last)中的元素,将容器中pred返回值为true的元素放在前面,false的元素放在后面,返回值为指向第二组元素的首指针。返回容器中的元素顺序不以一定与之前相同,有可能发生变化。

函数的源码类似于:

template <class BidirectionalIterator, class UnaryPredicate>
  BidirectionalIterator partition (BidirectionalIterator first,
                                   BidirectionalIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    while (pred(*first)) {
      ++first;
      if (first==last) return first;
    }
    do {
      --last;
      if (first==last) return first;
    } while (!pred(*last));
    swap (*first,*last);
    ++first;
  }
  return first;
}
  • 入参:

    • first, last:需要分治容器的起始与终止指针,类型为:Forward iterators

    • pred,一元函数,入参为容器元素,返回值为bool类型

  • 返回值:

    • 指向第二组元素的首指针
  • 示例

#include<iostream>
#include<vector>
#include<climits>
#include<algorithm>

using namespace std;


int main(int argc, char **argv)
{
    vector<int> rv(10, 0);
    for(int i=0; i < 10; i++) {
        rv[i] = i;
    }
    auto isOdd = [](int x) {return x % 2 == 1;};
    std::vector<int>::iterator bound;
    bound = std::partition (rv.begin(), rv.end(), isOdd);
    // print out content:
    std::cout << "odd elements:";
    for (std::vector<int>::iterator it=rv.begin(); it!=bound; ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    std::cout << "even elements:";
    for (std::vector<int>::iterator it=bound; it!=rv.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    // odd elements: 9 1 7 3 5
    // even elements: 4 6 2 8 0

    return 0;
}

# 2.排序函数

  • sort函数
bool myfunction (int i,int j) { return (i<j); } // 升序排列
int myints[] = {32,71,12,45,26,80,53,33};
std::vector<int> myvector (myints, myints+8);  // 32 71 12 45 26 80 53 33
// using default comparison (operator <):
std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
std::sort (myvector.begin()+4, myvector.end(), myfunction);
  • min_element
  • max_element

ForwardIterator min_element ( ForwardIterator first, ForwardIterator last )

返回和输入都是迭代器

# 3.修改输入序列的函数

  • random_shuffle随机打散数组中的元素
  std::random_shuffle ( myvector.begin(), myvector.end() );
std::vector<int> myvector (8); // myvector: 0 0 0 0 0 0 0 0
std::fill (myvector.begin(),myvector.begin()+4,5); // myvector: 5 5 5 5 0 0 0 0
int n = 3;
int a[n];
std::fill(a, a+n, 20); 20, 20, 20

# 4.不修改输入序列的函数

  • for_each
// 函数声明 fn:一元函数
 for_each (InputIterator first, InputIterator last, Function fn)
struct Sum
{
    void operator()(int n) { sum += n; }
    int sum{0};
};

void myfunction (int i) {  // function:
  std::cout << ' ' << i;
}
vector<int> myvector = {1, 2, 3, 4};
for_each (myvector.begin(), myvector.end(), myfunction);
Sum s = for_each(myvector.begin(), myvector.end(), Sum());
// using std::find with vector and iterator:
std::vector<int> myvector (myints,myints+4);
std::vector<int>::iterator it;

it = find (myvector.begin(), myvector.end(), 30);
if (it != myvector.end())
  std::cout << "Element found in myvector: " << *it << '\n';
else
  std::cout << "Element not found in myvector\n";

(adsbygoogle = window.adsbygoogle || []).push({});

# 参考资料