Guest User

Untitled

a guest
Jul 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string.h>
  4. #include <map>
  5.  
  6.  
  7. typedef struct {
  8. char * name;
  9. } Test;
  10.  
  11. class Tester {
  12. /*I want the TestCompare to be in class Tester*/
  13. struct TestCompare {
  14. bool operator()(const Test & LQuery, const Test & RQuery) const {
  15. return strcmp(LQuery.name, RQuery.name) < 0;
  16. }
  17. };
  18. public:
  19. Tester() {};
  20. ~Tester() {};
  21. void addElement(const Test *key, const Test *value) {
  22. m.insert(std::make_pair((*key),value));
  23. }
  24.  
  25. void printElement() {
  26. for(auto &e:m){
  27. std::cout<< e.first.name << " " << e.second->name << std::endl;
  28. }
  29. }
  30. private:
  31. std::map<const Test, const Test*,TestCompare> m;
  32. };
  33.  
  34. int main()
  35. {
  36. Test k1 = {"1"};
  37. Test v1 = {"1"};
  38.  
  39. Test k2 = {"2"};
  40. Test v2 = {"2"};
  41.  
  42. Test k3 = {"3"};
  43. Test v3 = {"3"};
  44.  
  45. Tester tester;
  46. tester.addElement(&k1, &v1);
  47. tester.addElement(&k2, &v2);
  48. tester.addElement(&k3, &v3);
  49. tester.printElement();
  50. return 0;
  51. }
Add Comment
Please, Sign In to add comment