(九)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() );
  • fill函数

    • 声明
     void fill (ForwardIterator first, ForwardIterator last, const T& val);
    
    • 示例
    	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";


欢迎访问个人网络日志🌹🌹知行空间🌹🌹


参考资料

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值