Reputation: 350
I have an unordered_map
(as below, as a member of a class) that undergoes a large number of additions and removals of keys. Such additions and removals happen within member functions that have the following strategy. They populate a locally created unordered_map
(of the same type as the original member), and finally the keys and the associated vector values are transferred to the class member.
Is this strategy appropriate? Will the scope rules for the locally created unordered_map
have an effect on the class member? Thanks for the support!
typedef std::unordered_map<int, std::vector<int> > mapStruct;
class foo {
mapStruct DS;
void f() {
mapStruct DS_tmp;
DS_tmp[1].push_back(5);
DS_tmp[1].push_back(10);
...
...
mapStruct::iterator it = DS_tmp.begin();
while( it != DS_tmp.end() ) {
DS[it->first] = it->second;
}
}
};
Upvotes: 0
Views: 215
Reputation: 21003
Nothing in your code stops you from getting rid of DS_tmp
and using DS
directly in function f
. But if due to some reasons you want to first fill temporary local variable and then copy it to the member, you can use swap
instead of the while
loop (which will never end by the way, because you are not incrementing it
):
DS.swap(DS_tmp);
Upvotes: 1