Advertisement
Guest User

Untitled

a guest
May 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <cstring>
  4.  
  5. template <typename T, typename D>
  6. class Biection <T, D>{
  7. public:
  8.     D getPairX(T x);
  9.     T getPairY(D y);
  10.     void add(T x, D y);
  11. private:
  12.     std::map <T, D> _map;
  13. };
  14.  
  15.  
  16.  
  17. template <typename T, typename D>
  18. D Biection::getPairX(T x) {
  19.     std::map::iterator it = _map.find(x);
  20.     if(it != _map.end())
  21.         return it->second;
  22.     else{
  23.         std::string str = "Element not found!";
  24.         throw str;
  25.     }
  26. }
  27.  
  28. template <typename T, typename D>
  29. T Biection::getPairY(D y) {
  30.     for(std::map::iterator it = _map.begin(); it != _map.end(); ++it){
  31.         if(it->second == y)
  32.             return it->first;
  33.     }
  34.     std::string str = "Element not found!";
  35.     throw str;
  36. }
  37.  
  38. template <typename T, typename D>
  39. void Biection::add(T x, D y) {
  40.     try {
  41.         getPairX(x);
  42.     }catch (std::string e){
  43.         try{
  44.             getPairY(y);
  45.         }catch (std::string e){
  46.             _map.insert(std::pair(x, y));
  47.             return;
  48.         }
  49.     }
  50.     std::string str = "This biection already exists!";
  51.     throw str;
  52. }
  53.  
  54. int main() {
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement