C++中的各种类型的复制()都允许以不同的方式执行复制操作,它们都有自己的用途。这些都是在header
- strt_iter1: 指向源容器开头的指针,必须从那里开始复制元素。
- 结束语1: 指向源容器末尾的指针,直到必须复制元素的位置。
- strt_iter2: 指向目标容器开头的指针,指向必须开始复制元素的位置。
2.复制(strt_iter1,num,strt_iter2): 此版本的复制允许用户自由选择在目标容器中复制多少元素。它还需要3个参数:
- strt_iter1: 指向源容器开头的指针,必须从那里开始复制元素。
- 号码: 整数,指定从strt_iter1开始将多少数字复制到目标容器。如果输入负数,则不执行任何操作。
- strt_iter2: 指向目标容器开头的指针,指向必须开始复制元素的位置。
CPP
// C++ code to demonstrate the working of copy() // and copy_n() #include<iostream> #include<algorithm> // for copy() and copy_n() #include<vector> using namespace std; int main() { // initializing source vector vector< int > v1 = { 1, 5, 7, 3, 8, 3 }; // declaring destination vectors vector< int > v2(6); vector< int > v3(6); // using copy() to copy 1st 3 elements copy(v1.begin(), v1.begin()+3, v2.begin()); // printing new vector cout << "The new vector elements entered using copy() : " ; for ( int i=0; i<v2.size(); i++) cout << v2[i] << " " ; cout << endl; // using copy_n() to copy 1st 4 elements copy_n(v1.begin(), 4, v3.begin()); // printing new vector cout << "The new vector elements entered using copy_n() : " ; for ( int i=0; i<v3.size(); i++) cout << v3[i] << " " ; } |
输出:
The new vector elements entered using copy() : 1 5 7 0 0 0 The new vector elements entered using copy_n() : 1 5 7 3 0 0
3.复制_if(): 顾名思义,此函数根据 条件 “。这是在 第四个论点 A. 返回布尔值的函数 . 此函数接受4个参数,其中3个类似于copy(),还有一个附加函数,当返回true时,复制一个数字,否则不复制数字。 4.向后复制_(): 此函数开始将元素复制到目标容器中 从后面 继续复制,直到所有的数字都没有被复制。复制从“开始” strt_iter2 “但是朝着相反的方向。它还接受与copy()类似的参数。
CPP
// C++ code to demonstrate the working of copy_if() // and copy_backward() #include<iostream> #include<algorithm> // for copy_if() and copy_backward() #include<vector> using namespace std; int main() { // initializing source vector vector< int > v1 = { 1, 5, 6, 3, 8, 3 }; // declaring destination vectors vector< int > v2(6); vector< int > v3(6); // using copy_if() to copy odd elements copy_if(v1.begin(), v1.end(), v2.begin(), []( int i){ return i%2!=0;}); // printing new vector cout << "The new vector elements entered using copy_if() : " ; for ( int i=0; i<v2.size(); i++) cout << v2[i] << " " ; cout << endl; // using copy_backward() to copy 1st 4 elements // ending at second last position copy_backward(v1.begin(), v1.begin() + 4, v3.begin()+ 5); // printing new vector cout << "The new vector elements entered using copy_backward() : " ; for ( int i=0; i<v3.size(); i++) cout << v3[i] << " " ; } |
输出:
The new vector elements entered using copy_if() : 1 5 3 3 0 0 The new vector elements entered using copy_backward() : 0 1 5 6 3 0
5.使用插入器()复制:
在copy()操作之前,让我们先了解inserter()的语法。
inserter()被用作我们想要复制容器元素的目的地。
inserter()接受两个参数。第一个是任意类型的容器,第二个是容器中的迭代器。
它返回一个在任意类型的容器上工作的insert_迭代器实例。这个包装函数有助于创建insert_迭代器实例。键入%迭代器的名称需要知道容器的精确完整类型,这可能会很繁琐,并且会妨碍通用编程。使用此函数可以利用自动模板参数推断,使编译器为您匹配正确的类型。
inserter()的语法:
std::inserter(Container& x, typename Container::iterator it);x: Destination container where the new elements will be inserted.it: Iterator pointing to the insertion point.Returns: An insert_iterator that inserts elements into x at the position indicated by it.
使用inserter()复制的语法:
copy(strt_iter1, end_iter1, inserter(Container& x, typename Container::iterator it));
C++
// C++ code to demonstrate the working of copy() using inserter() #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector< int > v1 = {1, 5, 7, 3, 8, 3}; vector< int >::iterator itr; vector< int > v2; //using inserter() copy(v1.begin(), v1.end(), inserter(v2, itr)); cout << "The new vector elements entered using inserter: " ; for ( int i = 0; i < v2.size(); i++) cout << v2[i] << " " ; } |
输出:
The new vector elements entered using inserter: 1 5 7 3 8 3
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。