将信息保存到哈希表或向量中

saving information into hash table or vectors

本文关键字:向量 哈希表 信息 保存      更新时间:2023-10-16

我想知道哪一个更快哈希表或向量

如果我想循环遍历里面的所有信息并将其与当前数据进行比较,如果它已经在里面,我想打破循环。

的例子:

我有[{1,2},{1,2,3}],在循环中,我当前的新数据是{1,2}(它在我的向量或哈希表中),所以我将打破循环,如果我有{2,1},我也会打破循环。

如果所有元素都匹配,不管顺序如何,否则继续循环。如果哈希表要快得多,我可以有一个关于如何实现它的提示吗?因为我是c++新手

Hashtable将更好地工作,因为您可以创建键值对。唯一的条件是不能有多于一个键相同的组合。因此,表中不能有3,1和3,2,因为键是唯一的。

如果在lhs上有副本,那么最好使用vector

我将使用嵌套集,即std::set<std::set<int> >

#include <set>
#include <cassert>
typedef std::set<int> Entry;
typedef std::set<Entry> Table;
int main () {
  int e1[] = {1,2};
  int e2[] = {1,2,3};
  int e3[] = {2,1};
  int e4[] = {3,2};
  Table t;
  t.insert(Entry(e1, e1+2));
  t.insert(Entry(e2, e2+3));
  Table::iterator it;
  Table::iterator end = t.end();;
  // Search for 1,2
  it = t.find(Entry(e1, e1+2));
  // Should find it
  assert(it != end);
  // Search for 2,1
  it = t.find(Entry(e3, e3+2));
  // Should find it
  assert(it != end);
  // Search for 3,2
  it = t.find(Entry(e4, e4+2));
  // Should NOT find it
  assert(it == end);
}