STL::MAP的插入问题

lokeio 2005-10-09 03:03:08
我刚用MAP有好多功能不会用。

map<int,int> BmpFiles::GetShowList(map<int,int> rm)
{
map<int,int> mm,mm2;
map<int,int>::iterator im,im2;
int i = 1;

for(im = rm.begin();im != rm.end();im ++)
{
if(MainBmpFilesList[im->second - 1].BMPs != NULL)
{
mm.insert(make_pair(indexShowList ++,im->second));//这个不好
}
else
{
mm2 = GetShowList(MainBmpFilesList[im->second - 1].FileList);
for(im2 = mm2.begin();im2 != mm2.end();im2 ++)
{
mm.insert(make_pair(im2->first,im2->second));//这个不好
}
}
}
return mm;
}
//---------------------------------------------------------------------------


这段代码是把传入的RM把它变为一串连起来的数组。RM中存储的是一标。这段程序功能正常。但我觉得写得不是太好,因为它用了两个循环,不夠精简,请问有人可以帮我写得好些吗?

还有,请各位高人指点下STL的用法的文章和书籍给我参考参考。谢谢
...全文
218 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lokeio 2005-10-11
  • 打赏
  • 举报
回复
for(im2 = mm2.begin();im2 != mm2.end();im2 ++)
{
mm.insert(make_pair(im2->first,im2->second));
}
在这里,请问有没有办法不用这几句做到
mm = mm + mm2
请指教
wylove 2005-10-11
  • 打赏
  • 举报
回复
使用泛型算法试一下:
例如:merge函数等。尝试一下吧!
fiftymetre 2005-10-10
  • 打赏
  • 举报
回复
map BLOCK LIST
map EXPR, LIST
The map function evaluates the BLOCK or EXPR for each element of LIST, locally setting the $_ variable equal to each element. It returns a list of the results of each evaluation. Map evaluates BLOCK or EXPR in a list context. Each element of LIST may produce zero, one, or more elements in the output list.

map函数对LIST中的每一个元素都进行一次BLOCK或者EXPR计算。$_代表LIST中当前的元素。map函数返回的列表是由每一次计算所得到的结果组成。Map运算BLOCK或者EXPR的时候是处于列表上下文的。LIST中的每个元素通过运算,都会得到一个或多个值,或者不生成值。LIST中所有元素运算完毕后得到的所有值组成结果列表。

In a scalar context, map returns the number of elements in the results. In a hash context, the output list (a, b, c, d, ...) is cast into the form ( a => b, c => d, ... ). If the number of output list elements is not even, the last hash element will have an undefined value.

在标量上下文中,map返回结果表中元素的个数。在哈希表上下文中,输出的表(a,b,c,d,...)被分组成为哈希表的格式(a=>b,c=>d,...)。如果输出列表中的元素不成对儿,那么最后一个元素作为键将会对应一个undefined值。

Avoid modifying $ in map’s BLOCK or EXPR, as this will modify the elements of LIST. (If you need to modify $ without modifying LIST, use this simple work-around.) Also, avoid using the list returned by map as an lvalue, as this will modify the elements of LIST. (An lvalue is a variable on the left side of an assignment statement.) Some Perl hackers may try to exploit this feature, but I recommend that you avoid this confusing style of programming.

不要在map的BLOCK或者EXPR中修改$_,因为这会改变LIST中元素的值。(如果你想改变$_而不改变LIST,那么就使用这个 。)同样,避免用map函数返回的列表作为等式左值,因为这也会改变LIST中的元素。(左值就是赋值表达式中传递给等号左边的值)也许一些perl骇客们会努力开发这种特性,但我建议你不要使用这种令人迷惑的编程方式。

xlsue 2005-10-10
  • 打赏
  • 举报
回复
GetShowList(map<int,int> rm) //我是说这里用引用成本比较低
lokeio 2005-10-10
  • 打赏
  • 举报
回复
我想改的就是这几句
mm2 = GetShowList(MainBmpFilesList[im->second - 1].FileList);
for(im2 = mm2.begin();im2 != mm2.end();im2 ++)
{
mm.insert(make_pair(im2->first,im2->second));
}
mm2是一个MAP,我想把它加到mm后面。
healer_kx 2005-10-09
  • 打赏
  • 举报
回复
整数当然要传值了。
而且使用的时候,学习的时候可以用insert。

以后熟练了就用[]操作符。
xlsue 2005-10-09
  • 打赏
  • 举报
回复
还有,为什么要传值呢?成本很高啊
xlsue 2005-10-09
  • 打赏
  • 举报
回复
同意楼上的。有几个东西没看懂。
fangrk 2005-10-09
  • 打赏
  • 举报
回复
没看懂。
indexShowList 是什么含义?请举例说明一下。
xlsue 2005-10-09
  • 打赏
  • 举报
回复
代码好像有点乱啊!
希望之晨 2005-10-09
  • 打赏
  • 举报
回复
看c++ primer 第6章,基本的用法都有了。

64,685

社区成员

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

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