2013-04-05 37 views
3

嗨我想记录插入一个boost :: unordered_map插入提振无序地图

地图被定义为

boost::unordered_map<int,Input> input_l1_map; 

其中输入是类

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

} 

我读了增强文档说它的功能insert()插入数据到容器,但是当我编译它显示错误。

+1

将来,您应该告诉我们错误是什么。 – 2014-03-14 12:32:04

回答

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

0

最自然的方式,国际海事组织,写这将是

input_l1_map[id] = input; 

Allthough

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

就OK了。

另外,就会有用于存储在地图中对一个typedef:

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

现在你可以把它明确:

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

operator []和insert()虽然不做同样的事情。如果没有该键的值存在,插入将只插入新值。 operator []返回对现有值的引用(如果存在)或插入值(如果不存在)并返回对该值的引用。 – harmic 2014-02-25 04:14:30

1

插入需要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)); 

}