gpt4 book ai didi

c++ - 字符串对的最佳映射

转载 作者:行者123 更新时间:2023-11-28 03:22:11 24 4
gpt4 key购买 nike

我有很多字符串对,我正在寻找一种将这些字符串对中的字符串相互映射的好方法。
假设我有 str1str2 对,我需要为 str1str1< 返回 str2/em> 用于 str2
我知道我可以为此使用 map

map<string, string>

但如果我只是为此使用 std::map,我将需要将每个字符串存储两次作为键和值。
哪个是避免重复的最佳解决方案?是否有为此优化的特殊容器?

最佳答案

使用 boost::bidirectional_maphttp://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

关于c++ - 字符串对的最佳映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15109409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com