fork download
  1. #include <map>
  2. #include <string>
  3. #include <sstream>
  4. #include <iomanip>
  5. #include <iostream>
  6.  
  7. int main()
  8. {
  9. std::map< unsigned char, char > look_up ;
  10.  
  11. // populate the map with data from the stream containing the table
  12. {
  13. std::istringstream stream( "61=M \n 62=N \n 63=O \n 64=P \n 65=Q \n" ) ;
  14. std::string byte ;
  15. char separator ;
  16. char data ;
  17. while( stream >> std::setw(2) >> byte >> separator >> data && separator == '=' )
  18. {
  19. // look_up[ std::stoi(byte,nullptr,16) ] = data ;
  20.  
  21. std::istringstream stm(byte) ;
  22. int key ;
  23. stm >> std::hex >> key ;
  24. look_up[key] = data ;
  25. }
  26. }
  27.  
  28. // use the map to translate the input data
  29. {
  30. std::istringstream stm( "abcdexedcbayaedbc" ) ;
  31. unsigned char byte ;
  32. while( stm >> byte )
  33. {
  34. auto p = look_up.find(byte) ;
  35. if( p != look_up.end() ) std::cout << p->second ;
  36. else std::cout << char(byte) ; // lookup failed
  37. }
  38. }
  39. }
  40.  
Success #stdin #stdout 0s 3036KB
stdin
Standard input is empty
stdout
MNOPQxQPONMyMQPNO