fork download
  1. #include <vector>
  2. #include <map>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. class Base {
  7. int x;
  8. public:
  9. Base() {}
  10. virtual ~Base() {}
  11. virtual void displayx() {
  12. std::cout << x << "\n";
  13. }
  14. };
  15.  
  16. class Derived : public Base {
  17. };
  18.  
  19. typedef std::vector<Base*> Bases;
  20. typedef std::map<std::string, Bases> MyMap;
  21.  
  22. int main() {
  23. Base* tanuki = new Derived;
  24.  
  25. MyMap myMap;
  26. MyMap::iterator it = myMap.find("racoon");
  27. if (it == myMap.end())
  28. myMap["racoon"].push_back(tanuki);
  29. else
  30. it->second.push_back(tanuki);
  31.  
  32. // print displayx on all elements of racoon
  33. it = myMap.find("racoon");
  34. if (it != myMap.end()) {
  35. Bases::iterator end = it->second.end();
  36. for (Bases::iterator cur = it->second.begin(); cur != end; ++cur)
  37. (*cur)->displayx();
  38. }
  39. return 0;
  40. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
0