【发布时间】:2015-07-01 12:48:35
【问题描述】:
我正在尝试在 C++ 中使用unordered_map,这样,对于键我有一个int,而对于值有一对浮点数。但是,我不确定如何访问这对值。我只是想理解这个数据结构。我知道要访问我们需要与此无序映射声明类型相同的iterator 的元素。我尝试使用iterator->second.first 和iterator->second.second。这是访问元素的正确方法吗?
typedef std::pair<float, float> Wkij;
tr1::unordered_map<int, Wkij> sWeight;
tr1::unordered_map<int, Wkij>:: iterator it;
it->second.first // access the first element of the pair
it->second.second // access the second element of the pair
感谢您的帮助和时间。
【问题讨论】:
-
unordered_map是 C++11 标准的一部分,您可以使用std::代替tr1:: -
你也可以使用
std::get<0>(it->second)或std::get<0>(std::get<1>(*it))(两者都给出it->second.first,这是完全有效的) -
感谢您的建议。
标签: c++ c++11 unordered-map std-pair keyvaluepair