Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 增强Bimap以插入或修改_C++_Boost_Bimap - Fatal编程技术网

C++ 增强Bimap以插入或修改

C++ 增强Bimap以插入或修改,c++,boost,bimap,C++,Boost,Bimap,STL映射“[]”运算符可以插入新条目或修改现有条目 map<string, string> myMap; myMap["key1"] = "value1"; myMap["key1"] = "value2"; map-myMap; myMap[“key1”]=“value1”; myMap[“键1”]=“值2”; 我正在用boost::bimap重写一些代码,它是由STL-map实现的。有没有一种简单的方法来保持STL“[]”行为?我发现我必须写下面7行代码来替换原来的STL映

STL映射“[]”运算符可以插入新条目或修改现有条目

map<string, string> myMap;
myMap["key1"] = "value1";
myMap["key1"] = "value2";
map-myMap;
myMap[“key1”]=“value1”;
myMap[“键1”]=“值2”;
我正在用boost::bimap重写一些代码,它是由STL-map实现的。有没有一种简单的方法来保持STL“[]”行为?我发现我必须写下面7行代码来替换原来的STL映射代码(1行!)

bimap::left_迭代器itr=myBimap.left.find(“key1”);
if(itr!=myBimap.left.end()){
myBimap.左。替换_数据(itr,“值2”);
} 
否则{
myBimap.insert(bimap::value_type(“键1”、“值2”);
}
我想知道是否有像boost::bimap::insert_或_modify()这样的实用函数。

显示了如何通过对
bimap
模板参数使用
set_of
list_of
来模拟
std::map
包括其
操作符[]

#include <iostream>
#include <string>
#include <boost/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/list_of.hpp>

int main()
{
    using namespace std;    
    map<string, string> myMap;
    myMap["key1"] = "value1";
    myMap["key1"] = "value2";
    for (auto&& elem : myMap)
        std::cout << "{" << elem.first << ", " << elem.second << "}, ";
    std::cout << "\n";

    using namespace boost::bimaps;
    bimap<set_of<string>, list_of<string>> myMap2;
    myMap2.left["key1"] = "value1";
    myMap2.left["key1"] = "value2";
    for (auto&& elem : myMap2.left)
        std::cout << "{" << elem.first << ", " << elem.second << "}, ";
    std::cout << "\n";

    auto res1 = myMap2.left.find("key1");
    std::cout << "{" << res1->first << ", " << res1->second << "} \n";    
}
#包括
#包括
#包括
#包括
#包括
int main()
{
使用名称空间std;
地图我的地图;
myMap[“key1”]=“value1”;
myMap[“键1”]=“值2”;
用于(自动和元素:myMap)

std::cout如果我还需要
myMap2.left.find(“foo”)
myMap2.right.find(“bar”)
?看起来
list\u的
没有提供
find()
成员函数。并且将myMap2声明为
bimap
由于使用
[]
操作符而导致编译错误。
#include <iostream>
#include <string>
#include <boost/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/list_of.hpp>

int main()
{
    using namespace std;    
    map<string, string> myMap;
    myMap["key1"] = "value1";
    myMap["key1"] = "value2";
    for (auto&& elem : myMap)
        std::cout << "{" << elem.first << ", " << elem.second << "}, ";
    std::cout << "\n";

    using namespace boost::bimaps;
    bimap<set_of<string>, list_of<string>> myMap2;
    myMap2.left["key1"] = "value1";
    myMap2.left["key1"] = "value2";
    for (auto&& elem : myMap2.left)
        std::cout << "{" << elem.first << ", " << elem.second << "}, ";
    std::cout << "\n";

    auto res1 = myMap2.left.find("key1");
    std::cout << "{" << res1->first << ", " << res1->second << "} \n";    
}