根据插入顺序从STL SET读取元素? [英] Reading elements from STL SET on the basis of order of insertion?

查看:316
本文介绍了根据插入顺序从STL SET读取元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能根据在C ++中将这些元素插入SET的顺序迭代SET(i,从SET读取元素)?使用SET是必需的,我无法改变它。



使用额外的内存来存储元素id或插入序列号等需要大量内存。这种解决方案对我没有帮助。



我在Objective-C中读到了一些,NSOrderedSet提供了这种功能(我不知道Objective- C,NSOrderedSet)。可以在C ++中实现类似的功能吗?

Is there any possibility of iterating through SET (i,e reading elements from SET) on the basis of order of insertion of those elements into SET in C++? Using SET is requirement, I can't change that.

Usage of extra memory for storing element id or insertion sequence number etc will require a lot of memory. Solutions of this kind will not be helpful to me.

I read some where that in Objective-C, NSOrderedSet give this kind of functionality ( I do not know Objective-C, NSOrderedSet ). Can the similar functionality be achieved in C++?

推荐答案

如果插入顺序是你的问题,你可以免费使用std :: vector,但不能使用std: :set。



如果内存是你的问题,std :: vector使用的内存少于std :: set



如果性能是你的问题,std :: vector比std :: set更快



你是否有一个很好的理由让你使用std ::组?不,要求不计算在内。我说原因!
If insertion order is your problem, you get that with std::vector at no cost, but not with std::set.

If memory is your problem, std::vector uses less memory than std::set

If performance is your problem, std::vector is faster than std::set

Is there even a single good reason for you to use std::set? No, requirements don't count. I said good reason!


请在这个问题页面上看到答案:对于不同的程序运行,对象的顺序会发生变化吗? [ ^ 的不同运行。



在该页面上,Stefan Lang是建议根据您的要求定义订单的方式,我反对使用该集合。如果您使用该集合并同时关注元素的顺序,则清楚地表明您应该使用其他容器。在集合中,排序/排序只是用于提高性能的实现细节。集合的基本数学概念并不假设有任何顺序,但实现顺序是不可避免的,只要你有办法遍历元素集。



让我对我无法改变它表示怀疑。 要求可能与任何其他编程错误完全一样是错误的问题。这是软件开发人员最重要的技能,可以错误地拒绝任何需求,并将问题与开发需求的人员协调起来。毕竟,不要忘记,如果要求错误来自其他人,您将负责最终结果。这是公平的:这是因为你没有说出来。不要犯这个错误。



-SA
Please see the answers on this question page: Order of objects are changed for different runs of program?[^].

On that page, Stefan Lang was the one to suggest a way of defining the order per your requirements and I argued against using the set. If you use the set and at the same time care of the order of elements, it's a clear indication that you should use some other container. In a set, sorting/ordering is just the implementation detail used to improve performance. The fundamental mathematical notion of the set does not assume having any order, but in implementation ordering is unavoidable, as soon as you have a way to traverse the set of elements.

And let me express my doubt about "I cannot change it". The "requirements" can be the matter of mistake exactly as any other programming mistake. This is the most important skill of the software developer to turn down any requirements with mistake and reconcile the problems with people developing requirements. After all, don't forget that if the requirement mistake originate from someone else, you will be responsible for the final result. And that is fair enough: this is because you did not speak up. Don't do this mistake.

—SA


不,除非你已经插入它们已订购。

请参阅:设置为C ++参考 [ ^ ]。
No, unless you insert them already ordered.
See: "set at C++ reference"[^].


这篇关于根据插入顺序从STL SET读取元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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