插入以提升unordered_map

时间:2013-04-05 12:21:07

标签: c++ boost boost-unordered

您好我正在尝试将记录插入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()将数据插入到容器中,但是当我编译它时显示错误。

3 个答案:

答案 0 :(得分:3)

您找到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;

来自here

您应该使用this->input_l1_map.insert(std::make_pair(id, input));

答案 1 :(得分:1)

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));

}

答案 2 :(得分:0)

最自然的方式,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);
相关问题