copy算法的限制?【要发就发100分】

matrixdwy 2008-07-17 04:35:10
看书看到一个看不懂的地方(红色)
These algorithms let you move sequences around.
OutputIterator copy(InputIterator first, InputIterator
last, OutputIterator destination);

Using assignment, copies from [first, last) to destination, incrementing destination after each
assignment. This is essentially a “shuffle-left” operation, and so the source sequence must not contain
the destination.

这句话大概意思就是使用copy( )的时候是有限制的,但是我看不懂
谁能给我解释一下,或者举个例子,或者给我说说一些copy( )函数使用时的限制。
...全文
134 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lc19890326 2008-07-17
  • 打赏
  • 举报
回复

template<class InputIterator, class OutputIterator>
OutputIterator copy(
InputIterator _First,
InputIterator _Last,
OutputIterator _DestBeg
);
源序列不能包含目标位置 _DestBeg不能位于_First和_Last之间
matrixdwy 2008-07-17
  • 打赏
  • 举报
回复
多谢,结了。接分的同学对不起了。。
matrixdwy 2008-07-17
  • 打赏
  • 举报
回复
回LS,没看出来有什么问题
ivec1: 1 2 3 4 5
ivec2: 2 3 4 5
after copy
ivec2: 1 2 3 4
很正常啊,就是copy的时候越界了。请指点
seayea 2008-07-17
  • 打赏
  • 举报
回复
shuffle-left.
源序列中不要包含目标位置。
举例说明:
char caTest[20];
把caTest[0]~caTest[9],拷贝到caTest[10]~caTest[19],这样没问题。
但是,如果把caTest[0]~caTest[9]拷贝caTest[5]~caTest[14]就有问题。
因为shuffle-left,循环copy一个char过去,source secquence中的10~14位已经
被先前的五个char覆盖了。 这是发生了那句话说的source secquence中有了destination。

这个note是透露了copy的细节。否则,如果copy内部是新分配了一部分空间,完全拷贝
了source,再写回到destination,那么就不会有这种问题了。



yuzl32 2008-07-17
  • 打赏
  • 举报
回复
换句话说,不能这样赋值
copy(a+1,a+9,a+2); 目标和源都属于同一个数组上,2者重叠了。
bargio_susie 2008-07-17
  • 打赏
  • 举报
回复
该说的原文也说了,楼上的各位也说了。 要不LZ来看段出问题代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
int a[5] = {1, 2, 3, 4, 5};
vector <int> ivec1(a, a+sizeof(a)/sizeof(a[0]));
vector <int> ivec2(ivec1.begin()+1, ivec1.end());


copy(ivec1.begin(), ivec1.end(), ivec2.begin());

vector<int>::iterator iter = ivec2.begin();

while (iter != ivec2.end())
{
cout << *iter << " ";
iter++;
}

return 0;
}
ouyh12345 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 bargio_susie 的回复:]
那就接分了
[/Quote]
ttkk_2007 2008-07-17
  • 打赏
  • 举报
回复

int main()
{
char sou[] = "test";
char des[100];
copy(sou, sou + 4, des);
cout << des << endl; //没问题
char *d = sou + 2; //重叠了
copy(sou, sou + 4, d);
cout << sou << endl; //把sou也给改了
cout << d << endl;
return 0;

}
//个人理解
cwc270 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ttkk_2007 的回复:]
应该是防止内存重叠
[/Quote]
应该是这意思,以前本人在sun的unix服务器上编程时曾经出现过类似现象,用memcpy时,源和目标是一样的情况下,拷贝长度比较长时,造成拷贝混乱。
coverallwangp 2008-07-17
  • 打赏
  • 举报
回复
This is essentially a “shuffle-left” operation, and so the source sequence must not contain
the destination.

这本质上是一个左移操作,所以源序列不能包含目标序列。
matrixdwy 2008-07-17
  • 打赏
  • 举报
回复
不。。。。其实还是没明白。。。。。
bargio_susie 2008-07-17
  • 打赏
  • 举报
回复
那就接分了
coverallwangp 2008-07-17
  • 打赏
  • 举报
回复
只有接分了
ttkk_2007 2008-07-17
  • 打赏
  • 举报
回复
应该是防止内存重叠
matrixdwy 2008-07-17
  • 打赏
  • 举报
回复
谁先来我就把分给了
matrixdwy 2008-07-17
  • 打赏
  • 举报
回复
不好意思,我自己明白了。。。

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧