简体   繁体   中英

How to update the vectors in a std::unordered_map<std::string, std::vector<int>> without copying?

How to extend the below code to collect the all entry.score and not just keep the most recent one?

The code I have right now is:

std::unordered_map<std::string, float> container;

int main() {
    // some code
    for (auto entry : entries)
        container[entry.name] = entry.score;
}

How to extend it to keep all entry.score whilst avoiding copy?:

std::unordered_map<std::string, std::vector<float>> container;
//vectors need to be updated using .push_back(entry.score)

int main() {
    // some code
    for (auto entry : entries)
        container[entry.name] = ;  //missing connection
}

container[entry.name] returns a std::vector<float>& . std::vector exposes push_back , which inserts an element into the vector.

Therefore you can do:

container[entry.name].push_back(entry);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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