Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string.h>
- #include <map>
- typedef struct {
- char * name;
- } Test;
- class Tester {
- /*I want the TestCompare to be in class Tester*/
- struct TestCompare {
- bool operator()(const Test & LQuery, const Test & RQuery) const {
- return strcmp(LQuery.name, RQuery.name) < 0;
- }
- };
- public:
- Tester() {};
- ~Tester() {};
- void addElement(const Test *key, const Test *value) {
- m.insert(std::make_pair((*key),value));
- }
- void printElement() {
- for(auto &e:m){
- std::cout<< e.first.name << " " << e.second->name << std::endl;
- }
- }
- private:
- std::map<const Test, const Test*,TestCompare> m;
- };
- int main()
- {
- Test k1 = {"1"};
- Test v1 = {"1"};
- Test k2 = {"2"};
- Test v2 = {"2"};
- Test k3 = {"3"};
- Test v3 = {"3"};
- Tester tester;
- tester.addElement(&k1, &v1);
- tester.addElement(&k2, &v2);
- tester.addElement(&k3, &v3);
- tester.printElement();
- return 0;
- }
Add Comment
Please, Sign In to add comment