STL MAP复制问题

gaomingok 2008-11-10 11:46:44
如何将MAP1中的内容复制到MAP2中,MAP1中内容仍然保留?
...全文
2649 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaomingok 2008-11-12
  • 打赏
  • 举报
回复
用结构体作为键和值,原来直接赋值就可以了,谢谢各位了
太乙 2008-11-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 baihacker 的回复:]

c1 = c2 Assigns all elements of c2 c1
Operation Effect
c.insert(elem) Inserts a copy of elem and returns the position of the new element and, for maps, whether it succeeded
c.insert(pos,elem) Inserts a copy of elem and returns the position of the new element (pos is used as a hint pointing to where the insert should start the search)
c.insert(beg,end) Inserts a copy of all elements…
[/Quote]
mm出来行动了??
呵呵~~~~
baihacker 2008-11-11
  • 打赏
  • 举报
回复

c1 = c2 Assigns all elements of c2 c1
Operation Effect
c.insert(elem) Inserts a copy of elem and returns the position of the new element and, for maps, whether it succeeded
c.insert(pos,elem) Inserts a copy of elem and returns the position of the new element (pos is used as a hint pointing to where the insert should start the search)
c.insert(beg,end) Inserts a copy of all elements of the range [beg,end)(returns nothing)
太乙 2008-11-11
  • 打赏
  • 举报
回复


因为map是一个双向迭代器,对=进行了重新的定义(重载)

所以通过迭代器来改变元素不大好使!

template<class InIt, class OutIt>
OutIt copy(InIt first, InIt last, OutIt x);
The template function evaluates *(x + N) = *(first + N)) once for each N in the range [0, last - first), for strictly increasing values of N beginning with the lowest value. It then returns x + N. If x and first designate regions of storage, x must not be in the range [first, last).


如果一定要用的话,得改写一下pair类!

lz可以试试~~

太乙 2008-11-11
  • 打赏
  • 举报
回复
#include<iostream>

#include<map>
#include<string>
using namespace std;
int main()
{
map<int,string> m1,m2;
m1[0]="hello";
m1[1]="world";
m2 = m1;
cout<<m2[1]<<endl;

return EXIT_SUCCESS;
}

如果lz想用什么copy泛型函数,肯定不行,因为map内部的结构不允许!!

lzr4304061988012 2008-11-11
  • 打赏
  • 举报
回复

#include <iostream>
#include <map>


using namespace std;


void main()
{
map<int,int> map1;



map<int,int> map2(map1);

}


lily604 2008-11-11
  • 打赏
  • 举报
回复
纯学习
Longinc 2008-11-11
  • 打赏
  • 举报
回复
学习
once_and_again 2008-11-11
  • 打赏
  • 举报
回复
对迭代器 的操作,比如 插入,排序会改变容器的内容,

比如 复制,查找,应该不会吧?不然闲着没事.
R_hgt 2008-11-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 oliver_wei 的回复:]
用STL中得COPY函数就可以,而且速度比较快。
[/Quote]

能否说的详细点?
oliver_wei 2008-11-11
  • 打赏
  • 举报
回复
用STL中得COPY函数就可以,而且速度比较快。
xiaopoy 2008-11-11
  • 打赏
  • 举报
回复
hmm
纯帮顶

http://blog.csdn.net/bat603/archive/2006/12/23/1456141.aspx

也不知复制的有用没。。

64,637

社区成员

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

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