Advertisement
obernardovieira

[Test] Change struct value inside std::map

Feb 24th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <functional>
  4.  
  5. struct teste {
  6.     int n;
  7. };
  8.  
  9. int main() {
  10.     std::map<char, teste> m;
  11.     teste tst;
  12.     tst.n = 5;
  13.     m.insert(std::make_pair('c', tst));
  14.  
  15.     std::map<char, teste>::iterator it1 = m.find('c');
  16.     if (it1 != m.end()) {
  17.         teste tst1 = it1->second;
  18.         std::cout << tst1.n << std::endl;
  19.     }
  20.  
  21.     std::map<char, teste>::iterator it2 = m.find('c');
  22.     if (it2 != m.end()) {
  23.         teste tst2 = it2->second;
  24.         tst2.n = 42;
  25.         it2->second = tst2;
  26.     }
  27.  
  28.     std::map<char, teste>::iterator it3 = m.find('c');
  29.     if (it3 != m.end()) {
  30.         teste tst3 = it3->second;
  31.         std::cout << tst3.n << std::endl;
  32.     }
  33.  
  34.     system("pause");
  35.     return 1;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement