简体   繁体   English

C ++ std :: map如何通过索引位置访问键

[英]C++ std::map how to access keys by index location

I would like to use C++ std::map to access the value associated to a given key in log(n) time. 我想使用C ++ std::map在log(n)时间访问与给定键关联的值。 Since the keys of a std::map are sorted, technically, I can access the keys by the location in the sorted order. 由于对std::map的键进行了排序,因此从技术上讲,我可以按排序顺序按位置访问键。 I know std::map does not have a random access iterator. 我知道std :: map没有随机访问迭代器。 Is there any "map like" data structure providing both access through the keys (by using the [] operator) and also providing the (read-only) random access though the location of the key in the sorted order. 是否有任何“像地图一样”的数据结构既提供通过键的访问(通过使用[]运算符),又通过键的位置按排序顺序提供(只读)随机访问。 Here is a basic example: 这是一个基本示例:

my_fancy_map['a'] = 'value_for_a'
my_fancy_map['b'] = 'value_for_b'

assert my_fancy_map.get_key_at_location(0) == 'a'
assert my_fancy_map.get_key_at_location(1) == 'b'
assert my_fancy_map.get_value_at_location(1) == 'value_for_b'
assert my_fancy_map['a'] == 'value_for_a'

You can use Boost.MultiIndex's ranked indices : 您可以使用Boost.MultiIndex的排名索引

Live On Coliru 生活在Coliru

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ranked_index.hpp>
#include <boost/multi_index/member.hpp>

using namespace boost::multi_index;

template<typename K,typename T>
using ranked_map=multi_index_container<
  std::pair<K,T>,
  indexed_by<
    ranked_unique<member<std::pair<K,T>,K,&std::pair<K,T>::first>>
  >
>;

#include <cassert>
#include <string>

int main()
{
  ranked_map<std::string,std::string> m;

  m.emplace("a","value for a");
  m.emplace("b","value for b");

  assert(m.nth(0)->first=="a");
  assert(m.nth(1)->first=="b");
  assert(m.nth(1)->second=="value for b");
  assert(m.find("a")->second=="value for a");
}

Note, however, that nth is not O(1) but logarithmic, so ranked indices are not exactly random-access. 但是请注意, nth不是O(1),而是对数,因此排名索引并非完全是随机访问的。

Postscript: Another alternative with true random access is Boost.Container's flat associative containers : 后记:另一个具有真正随机访问权限的替代方法是Boost.Container的扁平关联容器

Live On Coliru 生活在Coliru

#include <boost/container/flat_map.hpp>
#include <cassert>
#include <string>

int main()
{
  boost::container::flat_map<std::string,std::string> m;

  m["a"]="value for a";
  m["b"]="value for b";

  assert(m.nth(0)->first=="a");
  assert(m.nth(1)->first=="b");
  assert(m.nth(1)->second=="value for b");
  assert(m["a"]=="value for a");
}

The downside here is that insertion takes linear rather than logarithmic time. 这里的缺点是插入需要线性而不是对数时间。

You could just iterate through them: 您可以遍历它们:

my_fancy_map['a'] = 'value_for_a'
my_fancy_map['b'] = 'value_for_b'

auto location = std::begin(my_fancy_map);
assert location.first == 'a'
++location;
assert location.first == 'b'
assert location.second == 'value_for_b'
assert my_fancy_map['a'] == 'value_for_a'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM