从unordered_set获取最后一个元素 [英] Get Last element from unordered_set

查看:1654
本文介绍了从unordered_set获取最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从unordered_set中获取最后一个元素,并且它必须使用unordered_set,而不是任何其他类。 (主要是因为我要修改大量已经完成的代码),但按照我一直在寻找的方式,只能遍历它并保存元素,然后返回它。但是在大集合中它会太慢。

  unordered_set< int> :: iterator it = frames.end(); 
--it;

我得到以下错误:
'operator-'不匹配' - $'
$ b

它的一种有用的方式主要是因为它,它以堆栈的方式存储数据,如下所示:

  unordered_set< int> S; 
s.insert(9);
s.insert(4);
s.insert(8);
s.insert(0);
s.insert(1);

unordered_set< int> :: iterator it = s.end(); (it = s.begin(); it!= s.end(); ++ it)
cout<< *(它)< ;

打印:1 0 8 4 9



因此,last元素总是9,它是插入的第一个元素,正如我之前所说的以堆栈方式。



有什么想法可以改进它?

解决方案

在unordered_set中,插入顺序不一定与您的顺序相对应将在迭代时获得(因此名称为无序)。在此数据结构中不支持双向迭代器(使用 - 运算符)的部分原因是因为能够在unordered_set上进行反向/前向转换时,您不知道什么顺序元素,你会摆脱它。



您创建的插入顺序并不决定您在迭代时获得的顺序(首先插入9并不意味着s.end()会返回一个9)。这是因为指令的顺序完全取决于该组如何计算每个插入对象的哈希值,类似于哈希表( http://en.wikipedia.org/wiki/Hash_table )。因此,你不能可靠地使用这个集合来复制一个堆栈,因为这不是这个特定的数据结构的意图。



还有其他C ++ STL数据结构,您可以使用它们来保存命令,例如 http://www.cplusplus.com/reference/堆叠/

I need to get the last element from an unordered_set, and it must be using the unordered_set, not any other class. (mostly because I'm to modify lots of code already done) but by what I've been looking the only possible way is iterate over it and save the element and then return it. But in big sets it would be too slow. besides I tried this and did not work.

unordered_set <int>::iterator it = frames.end();
--it;

I got the following error: "no match for 'operator--' in '--it'"

Its kind of useful mostly because of this, it stores the data in a "stack" way, as following:

unordered_set<int> s;
s.insert(9);
s.insert(4);
s.insert(8);
s.insert(0);
s.insert(1);

unordered_set<int>::iterator it = s.end();
for( it = s.begin();  it!= s.end(); ++it )
    cout << *(it) << " ";

it prints:"1 0 8 4 9"

So the "last" element would be always 9, it is the "first" element that was inserted, as I said before in a "stack" way.

Any idea to improve it?

解决方案

In an unordered_set, the order of inserts does not necessarily correspond to the order that you will get when the set is iterated (hence the name "unordered"). Part of the reason why a bi-directional iterator is not supported(using a -- operator) in this data structure is because being able to go backwards/forwards on an unordered_set makes no difference when you don't know what the order of the elements that you will get out of it.

The order of inserts that you have created does not dictate the order you will get when you iterate (Inserting "9" first doesn't mean s.end() would return a "9"). This is because what dictates that order solely depends on how that set calculates the hash value of each object you insert, similar to a hash table (http://en.wikipedia.org/wiki/Hash_table). Hence, you cannot reliably use this set to replicate a "stack", as this is not what this particular data structure is meant to be used for.

There are other C++ STL data structures that you can use to preserve order such as http://www.cplusplus.com/reference/stack/.

这篇关于从unordered_set获取最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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