为什么map< T> :: erase(map ::< T> iterator)的g ++ 4.0版本不返回迭代器? [英] Why does the g++ 4.0 version of map<T>::erase(map::<T> iterator) not return a iterator?

查看:138
本文介绍了为什么map< T> :: erase(map ::< T> iterator)的g ++ 4.0版本不返回迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个中等规模的C ++项目从Visual Studio 2005移植到MacOS,XCode / GCC 4.0。

我刚刚遇到的一个差异是去掉地图中的元素。在Visual Studio中,我可以擦除由迭代器指定的元素,并将返回值分配给迭代器以获取下一个元素的位置。这样迭代器就不会在擦除后指向一些无效的地址。

换句话说,在Visual Studio中我可以这样做:



itor = m_ResourceMap.erase(itor);

在GCC 4.0中,擦除函数返回void,所以我不能这样做。这是否意味着下面的地图元素向后移动一个,所以迭代器自动指向下一个元素,或者这意味着我必须在之后增加迭代器? 在线STL文档在主题上并不十分简洁,而且XCode看起来不像有任何。



感谢您的帮助,

Adrian

解决方案

没有。擦除前必须增加迭代器。像这样

  m_ResourceMap.erase(itor ++); 

迭代器被擦除失效,所以你不能在之后增加它。


I'm porting a medium-sized C++ project from Visual Studio 2005 to MacOS, XCode / GCC 4.0.

One of the differences I have just stumbled across has to do with erasing an element from a map. In Visual Studio I could erase an element specified by an iterator and assign the return value to the iterator to get the position of the next element. This way the iterator would not point to some invalid address after erasing.

In other words, in Visual Studio I could do this:

itor=m_ResourceMap.erase(itor);

In GCC 4.0, the erase function returns void, so I can't do that. Does that mean that the following map elements are shifted one backwards, so the iterator points automatically to the next element, or does that mean that I have to increment the iterator afterwards? The online STL Documentation is not very concise on the subject and XCode does not seem to have any.

Thanks for your help,

Adrian

解决方案

No. You have to increment the iterator before erasing. Like this

m_ResourceMap.erase(itor++);

The iterator is invalidated by the erase, so you can't increment it afterwards.

这篇关于为什么map< T> :: erase(map ::< T> iterator)的g ++ 4.0版本不返回迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆