[英]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? 如何扩展以下代码以收集所有
entry.score
而不只是保留最新的?
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?: 如何扩展它以保留所有
entry.score
同时避免复制?:
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>&
. container[entry.name]
返回一个std::vector<float>&
。 std::vector
exposes push_back
, which inserts an element into the vector. std::vector
公开push_back
,它将一个元素插入到向量中。
Therefore you can do: 因此,您可以执行以下操作:
container[entry.name].push_back(entry);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.