赞
踩
template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
将范围[first,last)的数据拷贝到result中。注意last地址不拷贝
。
函数模板的功能等价于:
template<class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{
while (first!=last) {
*result = *first;
++result; ++first;
}
return result;
}
first、last:对应的是被拷贝序列的首地址和尾地址的后一个地址
。
Result:目标内存的首地址。
返回值:返回被拷贝序列的尾地址的后一个地址,既last
。
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int myints[] = { 10, 20, 30, 40, 50, 60, 70 };//被拷贝的数据 cout << "1.拷贝到vector容器中:" << std::endl; std::vector<int> myvector(10); std::vector<int>::iterator rtnIt = std::copy(myints, myints + 7, myvector.begin());//接收copy的返回值 std::cout << "vector容器值:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it){ std::cout << ' ' << *it; //测试返回的地址 if (it == rtnIt){ std::cout << "<-此处是返回的地址" << " "; } } std::cout << '\n'; std::cout << "----------------------------------------------------------------------------------"<<"\n"; cout << "2.拷贝数组中:" << std::endl; int* MyArr = new int[10]; int* rtnArr = std::copy(myints, myints + 7, MyArr); std::cout << "数组值:"; for (int i = 0; i<10; i++){ cout.width(8);//改变输出宽度 std::cout << MyArr[i] << " "; } std::cout << '\n'; std::cout << "数组地址:"; for (int i = 0; i<10; i++){ std::cout << &MyArr[i] << " "; } std::cout << '\n'; std::cout << "返回的地址:" << rtnArr<<"\n"; system("pause"); return 0; }
结果:注意红框处,它表明了copy()返回值的情况。
注意:使用std::copy时会报错,解决方法是:在预处理器加入(项目属性—>C/C++—>预处理—>预处理器定义):_SCL_SECURE_NO_WARNINGS。
template <class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward (BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result);
相比copy()而言,是从后向前拷贝
其功能等价于:
template<class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward( BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result )
{
while (last!=first)
*(--result) = *(--last);
return result;
}
first、last:对应的是被拷贝序列的首地址和尾地址的后一个地址
。
result:目标内存的尾地址。
返回值:返回被拷贝序列的首地址,既first
。
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int myints[] = { 10, 20, 30, 40, 50, 60, 70 };//被拷贝的数据 cout << "1.拷贝数组中:" << std::endl; int* MyArr = new int[10]; int* rtnArr = std::copy_backward(myints, myints + 7, MyArr+10); std::cout << "数组值:"; for (int i = 0; i<10; i++){ cout.width(10);//改变输出宽度 std::cout << MyArr[i] << " "; } std::cout << '\n'; std::cout << "数组地址:"; for (int i = 0; i<10; i++){ cout.width(10); std::cout << &MyArr[i] << " "; } std::cout << '\n'; std::cout << "返回的地址:" << rtnArr << "\n"; system("pause"); return 0; }
打印结果:注意,红框中对比的是返回值
以上就是copy()和copy_backward()函数的内容了。
不过我们还需思考一个问题:如果目标地址的大小比被拷贝序列的小会发生什么情况呢?以copy_backward()为例:若int* MyArr = new int[10];的大小不是10,而改为6会发生什么?其打印的结果如下:
可以发现,尽管大小不够,但还是会自动向前推一个地址拷贝。这样是不好的,因为不按我们的意愿就修改了内存中的值
。这一点是值得注意的。
参考:
链接: copy()函数.
链接:copy_backward()函数
本人水平有限,欢迎各位码友批评指正。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。