How to add the results from the iteration to new multimap in c++?

How to insert result into a new multimap?? I am trying to search over dictionary container to find keyword given by the user and I need to iterate over the tempCollector to find distinct elements. But, I can’t seem to find a way to store the results in tempCollector.

//current container with all the data
multimap<string, DictionaryItem> dictionaryContainer;
void search(vector<string> input) {
    if (dictionaryContainer.empty()) {
        cout << "it's empty";
    int inputSize = input.size();
    string tempKeyword = input[0];
    //need to copy or insert the value and keys to the tempCollector
    multimap<string, DictionaryItem>tempCollector;       
    //iteration to find keyword; want to store the result in tempCollector
    auto its = dictionaryContainer.equal_range(tempKeyword);
    for (multimap<string, DictionaryItem>::iterator itr =its.first; itr != its.second; itr++) {


If you want to copy the whole its range:

tempCollector.insert(its.first, its.second);

If you want to copy each element:

for (auto itr =its.first; itr != its.second; itr++) {
    if (condition) {
        tempCollector.emplace(tempKeyword, itr->second);
        tempCollector.emplace(tempKeyWord, DictionaryItem(...));

Keep in mind (multi)map handles pairs of key/values as std::pair<Key,T> (aka value_type).
The multimap::insert method assumes you’ve already constructed the pair(s) to insert, while multimap::emplace will build them for you.

Leave a Reply

Your email address will not be published. Required fields are marked *