从四个 std::vector 对象中选择元素最多的一个
我有四个 std::vector 容器,它们都可能(或可能不)包含元素。我想确定其中哪个拥有最多的元素并随后使用它。
我尝试创建一个 std::map ,将它们各自的大小作为键,并将对这些容器的引用作为值。然后我在每个向量的 size() 上应用 std::max 来计算最大值并通过 std::map 访问它。
显然,一旦至少两个向量中有相同数量的元素,这就会给我带来麻烦。
有人能想出一个优雅的解决方案吗?
I have four std::vector containers that all might (or might not) contain elements. I want to determine which of them has the most elements and use it subsequently.
I tried to create a std::map with their respective sizes as keys and references to those containers as values. Then I applied std::max on the size() of each vector to figure out the maximum and accessed it through the std::map.
Obviously, this gets me into trouble once there is the same number of elements in at least two vectors.
Can anyone think of a elegant solution ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这件事你想得太严重了。你只有四个向量。您可以通过 3 次比较来确定最大向量。只需这样做:
编辑:
现在有了指针!
编辑(280Z28):现在有参考资料! :)编辑:
带有引用的版本将不起作用。帕维尔·米纳耶夫(Pavel Minaev)在评论中很好地解释了这一点:
指针版本可能是您的最佳选择:它快速、低成本且简单。
You're severely overthinking this. You've only got four vectors. You can determine the largest vector using 3 comparisons. Just do that:
EDIT:
Now with pointers!
EDIT (280Z28):Now with references! :)EDIT:
The version with references won't work. Pavel Minaev explains it nicely in the comments:
The pointer version is likely your best bet: it's quick, low-cost, and simple.
这是一个解决方案(除了 Pesto 过于直接的方法之外) - 出于解释目的,我避免使用
bind
和 C++0x lambda,但您可以使用它们来消除对单独函数的需要。我还假设两个向量具有相同数量的元素,选择哪一个是无关紧要的。Here's one solution (aside from Pesto's far-too-straightforward approach) - I've avoided
bind
and C++0x lambdas for explanatory purposes, but you could use them to remove the need for a separate function. I'm also assuming that with two vectors with an equal number of elements, which one is picked is irrelevant.这是我非常简单的方法。唯一感兴趣的是你只需要基本的 C++ 就可以理解它。
Here is my very simple method. Only interest is that you just need basic c++ to understand it.
这是 coppro 答案的修改版本,使用 std::vector 来引用任意数量的向量进行比较。
This is a modified version of coppro's answer using a std::vector to reference any number of vectors for comparison.
我完全赞成过度思考的事情:)
对于查找组中最高/最低元素的一般问题,我将使用带有比较器的priority_queue:
(无耻地从coppro复制,并修改......)
I'm all for over-thinking stuff :)
For the general problem of finding the highest/lowest element in a group, I would use a priority_queue with a comparator:
(copying shamelessly from coppro, and modifying...)
您可以使用 std::multimap。这允许使用相同的密钥进行多个条目。
You could use a std::multimap. That allows multiple entries with the same key.