二维unordered_map [英] Two dimensional unordered_map

查看:404
本文介绍了二维unordered_map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  typedef boost :: unordered_map< int,void *>一维节点
typedef boost :: unordered_map< int,OneDimentionalNodes>两个三维节点;

TwoDimentionalNodes节点;

这有效吗?



我不要因为unordered_maps的键是单个整数,所以不使用任何哈希函数。
它会编译,但是当我这样迭代时,尝试访问this-> hash_function()(k)时会崩溃;

  for(TwoDimentionalNodes :: iterator it = nodes.begin(); it!= nodes.end(); ++ it)
{
for(OneDimentionalNodes :: iterator it2 =节点[it-> first] .begin(); it2!=节点[it-> first] .end(); ++ it2)
{
//做东西
}
}

我也向其他拥有

$ b $的容器开放b

  • O(1)访问

  • O(n)迭代

  • 稀疏


解决方案

如果只需要迭代所有元素,并且不需要循环访问特定维度,那么您可以使用一个简单的货币对作为unordered_map的键,例如:

  typedef std :: pair< int ,int>坐标; 
typedef std :: unordered_map< Coordinates,void *>二维节点;

(注意,我使用STL代替Boost,unordered_map现在也成为标准STL的一部分)。 / p>

简单地得到一个特定的值即可

  twoDimensionalNodes [std: :make_pair(x,y)] 

(或者,如果您不确定该值,请使用find



要进行迭代,只需对无序地图进行迭代:

  for(auto it = twoDimensionalNodes.begin(); it!= twoDimensionalNodes.end(); ++ it)
{
std :: cout<< x =<<它-> first.first;
std :: cout<< y =<<它-> first.second;
std :: cout<< value =<<它->秒;
}

为了使其更具可读性,我更喜欢先从迭代器,例如:

  for(auto it = twoDimensionalNodes.begin(); it!= twoDimensionalNodes.end(); + + it)
{
坐标&坐标= it->第一;
std :: cout<< x =<<首先
std :: cout<< y =<<坐标秒;
std :: cout<< value =<<它->秒;
}

如果尺寸大于2,则使用std :: tuple或简单地编写自己的Coordinates类以用作地图的键。


typedef boost::unordered_map<int, void*> OneDimentionalNodes;
typedef boost::unordered_map<int, OneDimentionalNodes> TwoDimentionalNodes;

TwoDimentionalNodes nodes;

is this valid?

i don't use any hash functions since keys of the unordered_maps' are single integers. it compiles, but when i iterate it like this, it crashes while trying to access this->hash_function()(k);

for (TwoDimentionalNodes::iterator it= nodes.begin(); it != nodes.end() ; ++it)
{
   for(OneDimentionalNodes::iterator it2 = nodes[it->first].begin(); it2 != nodes[it->first].end() ; ++it2)
    {
   // do stuff
    }
}

i'm also open to other containers with

  • O(1) access
  • O(n) iteration
  • Sparse

解决方案

If you just need to iterator over all elements, and it is not required to loop over a specific dimension, then you could use a simple pair as key for your unordered_map, like this:

typedef std::pair<int,int> Coordinates;
typedef std::unordered_map<Coordinates,void *> TwoDimensionalNodes;

(notice I used STL instead of Boost, unordered_map is now also part of the standard STL).

Getting a specific value is simply writing:

twoDimensionalNodes[std::make_pair(x,y)]

(or use find if you're not sure if that value is in your map).

To iterate, just iterate over the unordered map:

for (auto it=twoDimensionalNodes.begin();it!=twoDimensionalNodes.end();++it)
   {
   std::cout << "x=" << it->first.first;
   std::cout << "y=" << it->first.second;
   std::cout << "value=" << it->second;
   }

To make it a bit more readable, I prefer getting the coordinates first from the iterator, like this:

for (auto it=twoDimensionalNodes.begin();it!=twoDimensionalNodes.end();++it)
   {
   Coordinates &coordinates = it->first;
   std::cout << "x=" << coordinates.first;
   std::cout << "y=" << coordinates.second;
   std::cout << "value=" << it->second;
   }

If you have more than 2 dimensions, use std::tuple, or simply write your own Coordinates class to be used as key for the map.

这篇关于二维unordered_map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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