繁体   English   中英

插入以增强无序地图

[英]insert to boost unordered map

嗨,我正在尝试将记录插入boost :: unordered_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));

}

写自己的最自然的方式是IMO

input_l1_map[id] = input;

Allthough

input_l1_map.insert({ id,input }); // C++11

也可以。

或者,存储在地图中的对将有一个typedef:

typedef boost::unordered_map<int,Input> InputMap;
InputMap input_l1_map;

现在你可以明确说明:

InputMap::value_type item(id, input);
input_l1_map.insert(item);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM