嗨,我正试图插入一个记录到boost::unordered_map
Map定义为
boost::unordered_map<int,Input> input_l1_map;
其中Input是类
class Input {
int id;
std::string name;
std::string desc;
std::string short_name;
std::string signal_presence;
std::string xpnt;
}
我使用一个函数来插入下面的记录
void RuntimeData::hash_table(int id,Input input)
{
this->input_l1_map.insert(id,input);
}
我读了boost文档,它说一个函数insert()
插入数据到容器,但当我编译它显示错误。
你在哪里找到这样的insert
方法?
std::pair<iterator, bool> insert(value_type const&);
std::pair<iterator, bool> insert(value_type&&);
iterator insert(const_iterator, value_type const&);
iterator insert(const_iterator, value_type&&);
template<typename InputIterator> void insert(InputIterator, InputIterator);
其中value_type
为
typedef Key key_type;
typedef std::pair<Key const, Mapped> value_type;
从这里您应该使用this->input_l1_map.insert(std::make_pair(id, input));
insert参数为value_type,定义为:
typedef std::pair<Key const, Mapped> value_type;
void RuntimeData::hash_table(int id,Input input)
{
this->input_l1_map.insert(std::make_pair(id,input));
}
在我看来,最自然的写法应该是
input_l1_map[id] = input;
Allthough
input_l1_map.insert({ id,input }); // C++11
也可以。
或者,可以为存储在映射中的对定义一个类型:
typedef boost::unordered_map<int,Input> InputMap;
InputMap input_l1_map;
现在可以显式了:
InputMap::value_type item(id, input);
input_l1_map.insert(item);