GCC Tree STL 数据容器

发布于 2024-09-24 17:53:01 字数 754 浏览 3 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋檐 2024-10-01 17:53:01

您不能在关联容器上使用 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 a for loop and use the erase method to remove the elements. See this similar question remove_if equivalent for std::map for more details.

櫻之舞 2024-10-01 17:53:01

所有标准的有序/关联容器(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).

最偏执的依靠 2024-10-01 17:53:01

因为多重集(以及集合)是不可变容器,即容器中的元素不能更改就地修改,因此可以删除它们,修改并(重新)插入。

例如,请参阅:

  • 不同的
  • 在此教程中设置相关答案第27.3.2.1节

在简单的关联容器中,
其中元素是键,
元素是完全不可变的;这
嵌套类型迭代器和
因此 const_iterator 是相同的。

Because multiset (and also set) are immutable containers, i.e. elements in the container cannot be changed modified in-place, they can be removed, modified, and (re-)inserted.

See, for example:

  • a different set related answer
  • section 27.3.2.1 in this tutorial

In simple associative containers,
where the elements are the keys, the
elements are completely immutable; the
nested types iterator and
const_iterator are therefore the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文