字符串对的最佳映射



我有很多字符串对,我正在寻找一种很好的方法将这些字符串对中的字符串映射到彼此
假设我有str1str2对,我需要为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/jitNY

0

最新更新