GCC Tree STL 数据容器
可能的重复:
remove_if 相当于 std::map
昨天我写了一个程序,它使用多重集来 像这样的元素:
std::multiset < boost::shared_ptr < CEntity > > m_Entities;
然后我尝试使用标准算法remove_if
,如下所示:
std::remove_if(m_Entities.begin, m_Entities.end(), MarkedForDestroy);
但是编译失败,因为如果我们在 GCC 4.4 中看到集合和多重集的实现,我们会看到:
typedef typename _Rep_type::const_iterator iterator;
typedef typename _Rep_type::const_iterator const_iterator;
我很震惊。我此时用谷歌搜索,更好的是我发现这与标准并不矛盾。套装也一样。
如果标准算法不起作用,这怎么能不矛盾呢?我怎样才能更好地更换容器?
Possible Duplicate:
remove_if equivalent for std::map
Yesterday i wrote a program, which use multiset for storing elements like this:
std::multiset < boost::shared_ptr < CEntity > > m_Entities;
Then i try to use standard algorithm remove_if
like this:
std::remove_if(m_Entities.begin, m_Entities.end(), MarkedForDestroy);
BUT compilation failed, because if we see in GCC 4.4 implementaion of set and multiset we see that:
typedef typename _Rep_type::const_iterator iterator;
typedef typename _Rep_type::const_iterator const_iterator;
I was shocked. I google this moment and better that i found that this does not contradict the standard. The same for set.
How this cannot contradict if standart algorithms doesnt work? How i can better change container?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能在关联容器上使用
std::remove_if
算法。您需要编写一个for
循环并使用erase
方法删除元素。有关更多详细信息,请参阅此类似问题remove_if相当于std::map。You can not use
std::remove_if
algorithm on an associative container. You need to a write afor
loop and use theerase
method to remove the elements. See this similar question remove_if equivalent for std::map for more details.所有标准的有序/关联容器(map、set、multimap、multiset)都有不可变的键(只是键而不是整个容器)。
至于为什么只需看看每次更改键的值时需要发生什么:键类型需要以某种方式通知它的容器它已更改(这将在容器和容器之间引入非常紧密且不必要的耦合) ;它是关键类型 - 更不用说对于基本类型来说是不可能的),因为需要对容器进行重新排序以保持其排序属性(这是一个相当大的开销)。
All standard ordered/associative containers (map, set, multimap, multiset) have immutable keys (just the keys not the entire container).
As to why just look at what would need to happen every time you would change the value of the key: the key type would need somehow to notify it's container that it got changed (which would introduce a very tight and unnecessary coupling between the container & it's key type - not to mention being impossible to do for fundamental types) because the container would need to be resorted so to keep it's ordering property (which is a pretty big overhead).
因为多重集(以及集合)是不可变容器,即容器中的元素不能
更改就地修改,因此可以删除它们,修改并(重新)插入。例如,请参阅:
Because multiset (and also set) are immutable containers, i.e. elements in the container cannot be
changedmodified in-place, they can be removed, modified, and (re-)inserted.See, for example: