程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> STL algorithm算法copy_if(8)

STL algorithm算法copy_if(8)

編輯:C++入門知識

STL algorithm算法copy_if(8)


 

std::copy_if

template 
  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 
#include 
#include 
#include 
using namespace std;
void copyif(){
    vector v1{1,5,7,8,9,10,14,15};
    vector v2{99,88};
    cout<運行截圖:(v2在copy_if之前resize(10))

 

\

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved