Answer the question
In order to leave comments, you need to log in
How to add all elements from a vector to a multimap container under a specific key?
Good afternoon. Let's say there is a vector:
vector<string> names = {"Ivan", "Alexandr", "Kirill", "Victor"};
multimap<int, string> users;
for (auto &i : names) {
users.insert({1, i}); //добавляем имена в группу 1 (добавляем элемент вектора как значение-ключа для ключа[1])
}
Answer the question
In order to leave comments, you need to log in
If you need multimap, I don't see any other way. But if there are a lot of elements, it is worth using a hint (the position parameter, where the element will approximately stand). Something like (did not check in the compiler, the code is almost guaranteed to be incorrect).
if (!names.empty()) {
Users::const_iterator hint = users.upper_bound(1);
for (auto &i : names) {
users.insert(hint, {1, i});
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question