STL algorithm算法copy_if(8)

原文地址:http://blog.csdn.net/qq844352155/article/details/39136169

function template

<algorithm>

std::copy_if

template <class InputIterator, class OutputIterator, class UnaryPredicate>
  OutputIterator copy_if (InputIterator first, InputIterator last,
                          OutputIterator result, UnaryPredicate pred);

Copy certain elements of range

Copies the elements in the range [first,last) for which pred returns true to the range beginning at result.

将符合要求的元素(对元素调用pred返回true的元素)复制到目标数组中。

The behavior of this function template is equivalent to:

1
2
3
4
5
6
7
8
9
10
11
12
13
template <class InputIterator, class OutputIterator, class UnaryPredicate>
  OutputIterator copy_if (InputIterator first, InputIterator last,
                          OutputIterator result, UnaryPredicate pred)
{
  while (first!=last) {
    if (pred(*first)) {
      *result = *first;
      ++result;
    }
    ++first;
  }
  return result;
}

Parameters

first, last
Input iterators to the initial and final positions in a sequence. The range copied is [first,last), which contains all the elements between first and last, including
the element pointed by first but not the element pointed by last.

InputIterator shall point to a type assignable to the elements pointed by OutputIterator.

要复制的序列范围。

result
Output iterator to the initial position of the range where the resulting sequence
is stored. The range includes as many elements as [first,last).

开始覆盖的位置。

pred
Unary function that accepts an element in the range as argument, and returns a value convertible to bool. The value returned indicates whether the element is to be copied (if true,
it is copied).

The function shall not modify any of its arguments.

This can either be a function pointer or a function object.

接受一个参数并且返回一个bool值的一元函数。

The ranges shall not overlap.

Return value

An iterator pointing to the element that follows the last element written in the result sequence.

返回一个指向最后一个覆盖的元素再后面的一个元素的迭代器。

例子:

#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;
void copyif(){
    vector<int> v1{1,5,7,8,9,10,14,15};
    vector<int> v2{99,88};
    cout<<"at first,v2=";
    for(int &i:v2)
        cout<<i<<" ";
    cout<<endl;

    v2.resize(10);//resize(10)!!
    auto it=copy_if(v1.begin(),v1.end(),v2.end()-5,[](int i){
                        return i%2==0;});//如果是偶数,则返回true
    cout<<"after copy_if(v1.begin(),v1.end(),v2.end()-5,[](int i){return i%2==0;})"<<endl;
    cout<<"v2=";
    for(int &i:v2)
        cout<<i<<" ";
    cout<<endl;
    cout<<"the return it is :*it="<<*it<<endl;

}

运行截图:(v2在copy_if之前resize(10))

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// copy_if example
#include <iostream>     // std::cout
#include <algorithm>    // std::copy_if, std::distance
#include <vector>       // std::vector

int main () {
  std::vector<int> foo = {25,15,5,-5,-15};
  std::vector<int> bar (foo.size());

  // copy only positive numbers:
  auto it = std::copy_if (foo.begin(), foo.end(), bar.begin(), [](int i){return !(i<0);} );
  bar.resize(std::distance(bar.begin(),it));  // shrink container to new size

  std::cout << "bar contains:";
  for (int& x: bar) std::cout << ‘ ‘ << x;
  std::cout << ‘\n‘;

  return 0;
}

Edit
& Run

Output:

bar contains: 25 15 5

Complexity

Linear in the distance between first and last: Applies pred to each element in the
range and performs at most that many assignments.

Data races

The objects in the range [first,last) are accessed.

The objects in the range between result and the returned value are modified.

Exceptions

Throws if any of pred, the element assignments or the operations on iterators throws.

Note that invalid arguments cause undefined behavior.

——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:[email protected]

2014-9-8

于GDUT

——————————————————————————————————————————————————————————————————

时间: 2024-11-06 11:25:39

STL algorithm算法copy_if(8)的相关文章

STL algorithm算法集合

algorithm意为"演算法",是C++的标准模版库(STL)中最重要的头文件之一,提供了大量基于迭代器的非成员模版函数. 编程语言 C++ 类    别 C++标准库 头文件 #include <algorithm> 命名空间 using namespace std; 目录 ? 不修改内容的序列操作: ? 修改内容的序列操作: ? 划分操作: ? 排序操作: ? 二分法查找操作: ? 集合操作: ? 堆操作: ? 最大/最小操作: 不修改内容的序列操作: adjacen

STL algorithm算法rmonve,rmove_if(47)

rmove原型: std::remove template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val); 查找的得到第一个元素的位置,然后从此位置开始遍历容器,将后面的元素依次前移,跳过和value相同值的元素,也就是说,所有和value相同值的元素都会被覆盖,而其他的元素都会依次前移. 返回值是理论

STL algorithm算法generate和generate_n(22)

今后的stl算法部分就不贴cpluplus的原文了,简要的介绍为主. generate原型: std::generate template <class ForwardIterator, class Generator> void generate (ForwardIterator first, ForwardIterator last, Generator gen); 该函数是使用gen函数产生的值填充范围内元素的值. 其行为类似如下: 1 2 3 4 5 6 7 8 template &l

STL algorithm算法is_partitioned(26)

is_partitioned原型: std::is_partitioned template <class InputIterator, class UnaryPredicate> bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred); 测试范围内的元素是否是以pred为准则的一个划分.如果是,则返回true,否则返回false. 划分的意思是说,对每个元素进行pred(*it),得

STL algorithm算法详解

选一些感觉实用的写一下 count()    返回等价于给定值的元素个数 count_if()    返回满足条件的冤死个数 find() find_if() find_if_not() for_each() min_element(Iterator begin , Iterator end)min_element(Iterator begin , Iterator end , compFunc op)max_element(Iterator begin , Iterator end)max_e

STL algorithm算法minmax,minmax_element(36)

minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T&> minmax (const T& a, const T& b); custom (2) template <class T, class Compare> pair <const T&,const T&> minmax (const

STL algorithm算法merge(34)

merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); custo

STL algorithm算法mov,move_backward(38)

move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (InputIterator first, InputIterator last, OutputIterator result); 该函数是将指定范围内的元素移动到从result开始的位置. move之后,[first,last)范围内的元素去留的具体实现由编译器决定. result不能是在[first,last)

STL algorithm算法is_permutation(27)

is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class ForwardIterator2> bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); predicate (2) template <class ForwardIter