Fre*_*Foo 8 c++ boost unordered-map bimap data-structures
我想用a替换a vector<string>和boost::unordered_map<string, size_t>映射字符串到前者的索引boost::bimap.
我bimap应该使用什么实例?到目前为止,我已经想到了
typedef bimap<
unordered_set_of<size_t>,
vector_of<string>
> StringMap;
Run Code Online (Sandbox Code Playgroud)
但我不确定我现在是否已经改变了收藏类型.另外,我想知道我是否应该更改关系类型的集合.一个vector_of_relation是我最好的选择,还是一个set_of_relation,或者只是默认?
要获得 size_t 和 std::string 之间的 bimap,其中您有〜常数(取决于散列和任何潜在冲突的成本),您需要使用 unordered_set_of:
#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <string>
#include <iostream>
#include <typeinfo>
int main(int argc, char* argv[]) {
typedef boost::bimap< boost::bimaps::unordered_set_of<size_t>, boost::bimaps::unordered_set_of<std::string> > StringMap;
StringMap map;
map.insert(StringMap::value_type(1,std::string("Cheese")));
map.insert(StringMap::value_type(2,std::string("Cheese2")));
typedef StringMap::left_map::const_iterator const_iter_type;
const const_iter_type end = map.left.end();
for ( const_iter_type iter = map.left.begin(); iter != end; iter++ ) {
std::cout << iter->first << " " << map.left.at(iter->first) << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
返回:
1 Cheese
2 Cheese2
Run Code Online (Sandbox Code Playgroud)
unordered_set 是 set 的 boost 版本,它使用哈希表而不是树来存储元素,请参阅Boost Unordered 文档。
查看Bimap example中的一个 bimap 示例的评论,我们有:
左侧地图视图的工作方式类似于 std::unordered_map< std::string, long >,给定国家名称,我们可以使用它在恒定时间内搜索人口
| 归档时间: |
|
| 查看次数: |
2172 次 |
| 最近记录: |