简体   繁体   English

如何将 std::unordered_map 向量元素从一个减去另一个并在 C++ 中更新它?

[英]How to subtract std::unordered_map vector elements from one to other and update it in C++?

I have two std::unordered_map s in C++ where the key is an Index (unsigned integer) and the value is a vector of real numbers.我在 C++ 中有两个std::unordered_map ,其中键是索引(无符号整数),值是实数向量。 The maps are defined as the following:这些映射定义如下:

std::unordered_map<Index, std::vector<Real>> outgoing_msgs;

std::unordered_map<Index, std::vector<Real>> outgoing_msgs_prev;

These 2 unordered_maps have the same structures, and size properties, and also they have the same key values, same vector size but the difference resides in the vector element values.这 2 个 unordered_map 具有相同的结构和大小属性,并且它们具有相同的键值、相同的向量大小,但不同之处在于向量元素值。

I want to obtain the subtraction of the vector values of outgoing_msgs (the first unordered_map) from the values of the vector of outgoing_msgs_prev (which is the second unordered_map) based on the index, which means that the subtraction of the vector will happen for the vectors that have same keys (Indexes) in both unordered maps.我想根据索引从outgoing_msgs_prev (这是第二个unordered_map)的向量的值中获得outgoing_msgs (第一个unordered_map)的向量值的减法,这意味着向量的减法将发生在向量在两个无序映射中具有相同的键(索引)。

Is it possible?可能吗?

What is the fastest way to do it?最快的方法是什么?

Do you mean something like你的意思是像

for(auto &[key, val]: outgoing_msgs)
{
   auto &prev{outgoing_msgs_prev[key]};
   for(std::size_t i{}; i < val.size(); ++i) val[i] -= prev[i];
}

? ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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