日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

stl中copy()函数_std :: copy_if()函数以及C ++ STL中的示例

發布時間:2023/12/1 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 stl中copy()函数_std :: copy_if()函数以及C ++ STL中的示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

stl中copy()函數

C ++ STL std :: copy_if()函數 (C++ STL std::copy_if() function)

copy_if() function is a library function of algorithm header, it is used to copy the elements of a container, it copies the certain elements (which satisfy the given condition) of a container from the given start position to another container from the given beginning position.

copy_if()函數是算法標頭的庫函數,用于復制容器的元素,它將容器的某些元素(滿足給定條件)從給定的開始位置復制到另一個容器,從給定的開始位置位置。

Note:To use copy_if() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.

注意:要使用copy_if()函數 –包括<algorithm>頭文件,或者您可以簡單地使用<bits / stdc ++。h>頭文件。

Syntax of std::copy_if() function

std :: copy_if()函數的語法

std::copy_n(iterator source_first, iterator source_end, iterator target_start, UnaryPredicate pred);

Parameter(s):

參數:

  • iterator source_first, iterator source_end – are the iterator positions of the source container.

    迭代器source_first,迭代器source_end –是源容器的迭代器位置。

  • iterator target_start – is the beginning iterator of the target container.

    迭代器target_start –是目標容器的開始迭代器。

  • UnaryPredicate pred – Unary function which accepts an element in the range as an argument, and returns a value convertible to bool.

    UnaryPredicate pred –一元函數,該函數接受范圍內的元素作為參數,并返回可轉換為bool的值。

Return value: iterator – it is an iterator to the end of the target range where elements have been copied.

返回值: 迭代器 –它是目標元素已復制到目標范圍末尾的迭代器。

Example:

例:

Input://declaring & initializing an int arrayint arr[] = { 10, 20, 30, 40, 50 };//vector declarationvector<int> v1(5);//copying 5 array elements to the vectorcopy_n(arr, 5, v1.begin());Output://if we print the valuearr: 10 20 30 40 50v1: 10 20 30 40 50

C ++ STL程序演示std :: copy_if()函數的使用 (C++ STL program to demonstrate use of std::copy_if() function)

In this example, we are copying only positive elements of the array to the vector.

在此示例中,我們僅將數組的正元素復制到向量。

//C++ STL program to demonstrate use of //std::copy_if() function #include <iostream> #include <algorithm> #include <vector> using namespace std;int main() {//declaring & initializing an int arrayint arr[] = { 10, 20, 30, -10, -20, 40, 50 };//vector declarationvector<int> v1(7);//copying 5 array elements to the vectorcopy_if(arr, arr + 7, v1.begin(), [](int i) { return (i >= 0); });//printing arraycout << "arr: ";for (int x : arr)cout << x << " ";cout << endl;//printing vectorcout << "v1: ";for (int x : v1)cout << x << " ";cout << endl;return 0; }

Output

輸出量

arr: 10 20 30 -10 -20 40 50 v1: 10 20 30 40 50 0 0

Reference: C++ std::copy_if()

參考: C ++ std :: copy_if()

翻譯自: https://www.includehelp.com/stl/std-copy_if-function-with-example.aspx

stl中copy()函數

總結

以上是生活随笔為你收集整理的stl中copy()函数_std :: copy_if()函数以及C ++ STL中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。