вставить, чтобы увеличить неупорядоченную карту

Привет я пытаюсь вставить запись в 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);

}

Я прочитал документацию надстройки, он говорит, что функция insert() вставить данные в контейнер, но когда я компилирую это показывает ошибку.

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;

от Вот

Вы должны использовать this->input_l1_map.insert(std::make_pair(id, input));

3

Другие решения

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

}
1

ИМО, самый естественный способ написать это было бы

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);
0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector