I've C++ STL map with value as another map. I want to updated the value of inner map.
When inner map values is update and print immediately, it show updated value. But after that when a display function is called, it doesn't show the updated value. Not sure what I'm missing.
#include <iostream>
#include <map>
typedef std::map <std::string, std::string> poolNameApnNameT ;
class A
{
public:
struct S
{
std::map <std::string, poolNameApnNameT>apnPoolValidationDb;
}s;
void PrintfMe();
};
void A::PrintfMe()
{
for(auto x: s.apnPoolValidationDb)
{
std::cout <<"main: "<<x.first << std::endl;
for(auto y: x.second)
{
std::cout <<"Inner: " <<y.first.c_str() <<" "<<y.second.c_str() << std::endl;
}
}
}
int main()
{
A a;
a.s.apnPoolValidationDb.emplace(
std::make_pair((char*)"pool1",
poolNameApnNameT({ std::make_pair((char*)"pool2","xx")})
));
a.s.apnPoolValidationDb.emplace(
std::make_pair((char*)"pool2",
poolNameApnNameT({ std::make_pair((char*)"pool1","zz")})
));
a.PrintfMe();
/* search key and updated value's value(inner map).*/
for(auto search: a.s.apnPoolValidationDb)
{
auto search2 = search.second.find("pool2");
if(search2 != search.second.end())
{
std::cout<<"found: " <<search2->first<<std::endl;
std::cout<<"Updating: " <<search2->second<<std::endl;
search2->second.assign((char*)"yy"); // Update using assign()
std::cout<<"After Update " <<search2->second.c_str()<<std::endl;
search.second[search2->first] = "OO"; // Update using []
std::cout<<"After Update " <<search2->second.c_str()<<std::endl;
}
}
std::cout<<std::endl;
a.PrintfMe();
return 0;
}
Output: main: pool1
Inner: pool2 xx main: pool2 Inner: pool1 zz
found: pool2 Updating: xx After Update yy After Update OO
main: pool1 Inner: pool2 xx main: pool2 Inner: pool1 zz <<< should be OO instead of zz
Aucun commentaire:
Enregistrer un commentaire