简体   繁体   English

如何在std :: unordered_map中更新向量 <std::string, std::vector<int> &gt;没有复制?

[英]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.

相关问题 在std :: unordered_map上进行迭代 <int, std::vector<Element> &gt; - Iterator over std::unordered_map<int, std::vector<Element>> C ++ 11:std :: unordered_map <int, std::stack<int> &gt;从地图获取值而不复制多次 - C++11: std::unordered_map<int, std::stack<int>> getting values from map without copying multiple times std :: vector或std :: list for std :: unordered_map存储桶? - std::vector or std::list for std::unordered_map buckets? unordered_map 中的段错误<int, std::vector<double> &gt; - Seg fault in unordered_map <int, std::vector<double>> 使用带预分配 std::unordered_map 的嵌套向量快速构造 unordered_map <int, vector<thing *> >?</int,> - Fast construction of unordered_map with nested vector with preallocation std::unordered_map<int, vector<Thing *>>? 排序std :: unordered_map <std::string, std::atomic<unsigned int> &gt;按值 - Sort std::unordered_map<std::string, std::atomic<unsigned int>> by values 如何在 C++ 11 及更高版本中将 std::map 和 std::unordered_map 的资源手动释放为 std::vector - How to release resources of std::map and std::unordered_map manually as std::vector in C++ 11 and higher 如何使用 boost::unordered_map 的 boost std::vector 序列化/反序列化 - How to serialize/deserialize with boost std::vector of boost::unordered_map 对std :: unordered_map进行Boost :: serialization <double, std::vector<double> &gt; - Boost::serialization of an std::unordered_map<double, std::vector<double>> 将std :: unordered_map值移动到std :: vector - Moving an std::unordered_map values to std::vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM