Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.43 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. I want to perform in place modification to a std::map while iterating over it
  2. #include <string>
  3. #include <map>
  4. struct EmployeeKey
  5. {
  6.     std::string name;
  7.     int amount;
  8.     int age;
  9. };
  10.  
  11. struct EmployeeDetail
  12. {
  13.     std::string dept;
  14.     int section;
  15.     int salary;
  16. };
  17.  
  18. bool compareByNameAge(const std::string& name,
  19.                       const int& age,
  20.                       const EmployeeKey& key )
  21. {
  22.     return name > key.name && age > key.age;
  23. }
  24.  
  25. typedef std::map<EmployeeKey, EmployeeDetail> EmployeeMap;
  26.  
  27. int main()
  28. {
  29.     EmployeeMap eMap;
  30.     // insert entries to the map
  31.     int age = 10;
  32.     std::string name = "John";
  33.  
  34.     EmployeeMap transformMap;
  35.     foreach( iter, eMap )
  36.     {
  37.         if ( compareByNameAge(name, age, iter->first) )
  38.         {
  39.             //**This is what i want to avoid.......
  40.             // take a copy of the data modified
  41.             // push it in a new map.
  42.             EmployeeDetail det = iter->second;
  43.             det.salary = 1000;
  44.             transformMap[iter->first] = det;
  45.         }
  46.     }
  47.  
  48.     //** Also, i need to avoid too...  
  49.     // do the cpy of the modified values  
  50.     // from the transform map to the  
  51.     // original map  
  52.     foreach( iter1, transformMap )  
  53.         eMap[iter1->first] = iter1->second;
  54. }
  55.        
  56. foreach( iter, eMap )  
  57.    {  
  58.        if ( compareByNameAge(name, age, iter->first) )  
  59.        {
  60.            iter->second.salary = 1000;
  61.        }  
  62.    }
  63.        
  64. foreach(iter, eMap)  
  65. {  
  66.    if (compareByNameAge(name, age, iter->first))  
  67.      iter->second.salary = 1000;  
  68. }
  69.        
  70. EmployeeDetail& det = iter->second;  
  71. det.salary = 1000;
  72.        
  73. EmployeeDetail& det = iter->second;   // notice new '&' character.
  74. det.salary = 1000;   // modifies the 'EmployeeDetail' object in-place.
  75.        
  76. it->second.salary = 1000;
  77.        
  78. class SalaryUpdater
  79. {
  80. public:
  81.     SalaryUpdater(const std::string& name, int age) : name_(name), age_(age) { }
  82.  
  83.     void operator()(EmployeeMap::value_type& item)
  84.     {
  85.         if(compareByNameAge(name_, age_, item.first))
  86.         {
  87.             item.second.salary = 1000;
  88.         }
  89.     }
  90.  
  91. private:
  92.     std::string name_;
  93.     int age_;
  94. };
  95.  
  96. int main()
  97. {
  98.     EmployeeMap eMap;
  99.     // insert entries to the map
  100.  
  101.     std::for_each(eMap.begin(), eMap.end(), SalaryUpdater("John", 10));
  102. }
  103.        
  104. for (auto& pair : eMap )
  105.   if (pair.first.name == "Rob")
  106.     pair.second.salary *= 1000;
  107.        
  108. for(EmployeeMap::iterator it = eMap.begin(); it != eMap.end(); ++it)
  109.   if(pair.first.name == "Rob")
  110.     pair.second.hours /= 2;