泛型算法名稱 |
copy() 拷貝 |
作用 |
將源容器中的數據拷貝到目標容器內,順序不變 |
參數 |
源容器開頭, 源容器結尾,目標容器開頭 |
泛型算法名稱 |
copy_backward() 逆向拷貝 |
作用 |
將源容器中的數據拷貝到目標容器內,順序不變 |
參數 |
源容器開頭, 源容器結尾,目標容器結尾 |
//創建動態數組
vector<int> CreateVector1()
{
vector<int> VeInt;
srand(time(0));
for (int i = 2; i < 12; i++)
{
VeInt.push_back(rand() % 100);
}
return VeInt;
}
//輸出動態數組
void ShowVector(vector<int> VeInt)
{
vector<int>::iterator showVeInt;
for (showVeInt = VeInt.begin(); showVeInt != VeInt.end(); showVeInt++)
{
cout << *showVeInt << " ";
}
cout << endl;
}
//拷貝算法
void my_copy(vector<int> &intvector, vector<int> &vector)
{
copy(intvector.begin(), intvector.end(), vector.begin());
}
//逆向拷貝算法
void my_copy_backward(vector<int> &intvector, vector<int> &vector)
{
copy_backward(intvector.begin(), intvector.end(), vector.end());
}
//主函數
void main()
{
vector<int> VeInt = CreateVector1();
vector<int> VeInt2(10);
vector<int> VeInt3(10);
my_copy(VeInt, VeInt2);
cout << "源:VeInt :" << "\t";
ShowVector(VeInt);
cout << "\n" << "copy的VeInt2 :" << "\t";
ShowVector(VeInt2);
my_copy_backward(VeInt, VeInt3);
cout << "\n" << "copy_backward的VeInt3 :" << "\t";
ShowVector(VeInt3);
}
逆向拷貝是從源容器的結尾開始進行拷貝,拷貝完的數據順序不變。