字符串对的最佳映射
Optimal mapping for pairs of string
我有很多字符串对,我正在寻找一种很好的方法将这些字符串对中的字符串映射到彼此
假设我有str1和str2对,我需要为str1返回str2,为str2返回str1。我知道我可以使用一个映射
map<string, string>
但是如果我简单地使用std::map,我将需要将每个字符串存储两次,作为键和值。
避免重复的最佳解决方案是什么?是否有专门针对此进行优化的容器?
使用boost::bidirectional_map
。http://www.boost.org/doc/libs/1_52_0/libs/bimap/doc/html/index.html
#include <boost/bimap/bimap.hpp>
#include <string>
#include <iostream>
int main()
{
namespace bi = boost::bimaps;
typedef bi::bimap<std::string, std::string> bimap;
bimap map;
map.insert(bimap::value_type("1", "2"));
std::cout << map.left.at("1") << std::endl;
std::cout << map.right.at("2") << std::endl;
}
http://liveworkspace.org/code/jitNY0
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C#中处理C++指针而不使用unsafe的最佳方法
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 遍历 STL 映射(集/多集)的最佳方法,同时元素可能会在循环期间被删除并重新插入?
- 在映射最小值和最大值时从未签名转换为已签名的最佳方法?
- 将映射从字符串到INT的最佳方法,而另一个同时进行的方式
- 初始化 C++11 数组的最佳方法,主要是标识映射
- 映射/联接两个自动生成的枚举的最佳方式
- 当映射中已知值时获取键的最佳方法
- 这是初始化值为std::向量的std::映射的最佳方法
- OpenCL缓冲区分配和映射最佳实践
- 将多个值存储为映射中的键的最佳方法是什么
- 字符串对的最佳映射
- 转换/映射方法最佳做法