Advertisement
obernardovieira

SaveVars(integer,string,integer) unordered_map

Sep 28th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <tuple>
  4. #include <utility>
  5. #include <unordered_map>
  6.  
  7. typedef std::tuple<std::string,int> key_t;
  8.  
  9. struct key_hash : public std::unary_function<key_t, std::size_t>
  10. {
  11.    std::size_t operator()(const key_t& k) const
  12.    {
  13.       return std::get<0>(k)[0] ^ std::get<1>(k);
  14.    }
  15. };
  16. struct key_equal : public std::binary_function<key_t, key_t, bool>
  17. {
  18.    bool operator()(const key_t& v0, const key_t& v1) const
  19.    {
  20.       return (
  21.                std::get<0>(v0) == std::get<0>(v1) &&
  22.                std::get<1>(v0) == std::get<1>(v1)
  23.              );
  24.    }
  25. };
  26.  
  27.  
  28.  
  29. class playervars {
  30. private:
  31.     typedef std::unordered_map<key_t,int,key_hash,key_equal> variable_i;
  32.     variable_i vari;
  33.     int numero;
  34. public:
  35.     void SetInt(int playerid, std::string nomevar,int valor) {
  36.         vari[std::make_tuple(nomevar,playerid)] = valor;
  37.     }
  38.     int GetInt(int playerid, std::string nomevar) {
  39.         auto itr = vari.find(std::make_tuple(nomevar,playerid));
  40.         if (vari.end() != itr)
  41.         {
  42.             return itr->second;
  43.         }
  44.     }
  45. };
  46.  
  47. playervars PVar;
  48.  
  49. int main()
  50. {
  51.     PVar.SetInt(5,"carro",10);
  52.  
  53.     std::cout << PVar.GetInt(5,"carro") << std::endl;
  54.  
  55.  
  56.     system("pause");
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement