pmcgee

map test

Jun 19th, 2020
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #pragma argsused
  2.  
  3. #include <string>
  4. #include <iostream>
  5. #include <iterator>
  6. #include <map>
  7.  
  8. using namespace std;
  9.  
  10. struct TTest   {
  11.     int     ID;
  12.     string  Name;
  13.     string  Show()  {  return  to_string(ID) + " - " + Name;    }
  14. };
  15.  
  16.  
  17. typedef map<string,TTest> TTestMapList;
  18.  
  19. //TTestMapList TestList;
  20.  
  21.  
  22. TTestMapList& GetTestList()   {
  23.     static TTestMapList TestList;
  24.     return TestList;
  25. }
  26.  
  27.  
  28. bool GetTest(string str, TTest& T) { // any changes made to T does not affect value in map
  29.     TTestMapList::iterator it = GetTestList().find(str);
  30.     bool res = (it != GetTestList().end());
  31.     //TTestMapList::iterator it = TestList.find(str);
  32.     //bool res = (it != TestList.end());
  33.     if (res)  T = it->second;
  34.     return res;
  35. }
  36.  
  37.  
  38. int main()  {
  39.     TTest s, t { 1, "abc" };
  40.  
  41.     //TestList.insert( pair<string, TTest> ("5", t) );
  42.     GetTestList().insert( pair<string, TTest> ("5", t) );
  43.  
  44.     if (GetTest("5", s))  {
  45.          s.ID   = 1985;
  46.          s.Name = "Ahmed Mido Sayed";
  47.     }
  48.  
  49.     cout << t.Show() << "\n";
  50.     cout << s.Show() << "\n";
  51.  
  52.     int x; cin >> x;
Add Comment
Please, Sign In to add comment