传送门 ==>> AutoSAR实战系列300讲「糖果Autosar」总目录
C++ STL 中存在各种不同的 copy(),它们允许以不同的方式执行复制操作,它们都有自己的用途。这些都在标题 中定义。本文向大家介绍这些函数,供日常编程使用。
copy(strt_iter1, end_iter1, strt_iter2) :用于将一系列元素从一个容器复制到另一个容器的通用复制函数。它需要 3 个参数:
strt_iter1 :指向源容器开头的指针,必须从那里开始复制元素。
end_iter1 :指向源容器末尾的指针,直到必须复制元素为止。
strt_iter2 :指向目标容器开头的指针,指向必须开始复制元素的位置。
copy_n(strt_iter1, num, strt_iter2) :此版本的副本允许自由选择必须在目标容器中复制多少元素。IT 还需要 3 个参数:
strt_iter1 :指向源容器开头的指针,必须从那里开始复制元素。
num :整数,指定从 strt_iter1 开始将多少个数字复制到目标容器。如果输入负数,则不执行任何操作。
strt_iter2 :指向目标容器开头的指针,指向必须开始复制元素的位置。
// C++ code to demonstrate the working of copy()
// and copy_n()
#include
#include // for copy() and copy_n()
#include
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
// C++ code to demonstrate the working of copy_if()
// and copy_backward()
#include
#include // for copy_if() and copy_backward()
#include
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
inserter() 的语法:
std::inserter(Container& x, typename Container::iterator it);
x:将插入新元素的目标容器。it:指向插入点的迭代器。
返回: 一个 insert_iterator 将元素插入到x 所指示的位置。
使用 inserter() 进行复制的语法:
copy(strt_iter1, end_iter1, inserter(Container& x, typename Container::iterator it));
// C++ code to demonstrate the working of copy() using inserter()
#include
#include
#include
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 << "\nThe 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