简体   繁体   中英

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. 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.

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.

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];
}

?

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