-4

I am trying to print out more than one record from a map that has a string and a pointer class. This is a follow up from this post: how to dereference a pointer of a map of pointers to objects in c++. 1. I am having issues on pushing back an object class into a vector. 2. I would like to print more than one record from a map.

I have tried to use vectors but not sure if that's the way to go. I got some errors from it. I know in Java you can use array list so trying to refresh my c++. Any good examples you have following from above link solution.

class Employee
{
    public:
    int  employeeID;

    Employee()
    {
       employeeID = 123;
    } 
 };
int main(int argc, char* argv[]) {
  std::map<std::string, Employee *> *_employeePayroll;
  std::map<std::string, Employee *> _employeeID;
  std::map<std::string, Employee *>::const_iterator itEmployeeID;

  _employeePayroll = &_employeeID;
  (*_employeePayroll)["Karl"] = new Employee;
  (*_employeePayroll)["George"] = new Employee;

  vector<std::string> v1;
  v1.push_back(itEmployeeID->first);
  // Here I am having issues on pushing back an object class into a 
  // vector.  maybe as follows:
  v1..push_back(itEmployeeID->second);
  v1.shrink_to_fit();
  for ( auto it = v1.begin(); it != v1.end(): it++ )
  {
     std::cout << *it;
     std::cout << std::endl;
  }

  return 0;
}

I would like to print out more than one record from a map that has a string and a pointer class.

user2537246
  • 143
  • 2
  • 10
  • i think its just a typo if(auto it..) should actually be for(auto it ...) so you can loop through it all, Second it != v1.end()';' not ':' – Spinkoo Feb 07 '19 at 23:37
  • Please provide an implementation of Employee. – Rietty Feb 07 '19 at 23:37
  • You can also write that line using a range-based `for` loop, e.g. `for (const auto& x : v1 )`. – Davislor Feb 07 '19 at 23:45
  • The implementation is in this link which is a continuation: https://stackoverflow.com/questions/53755459/how-to-dereference-a-pointer-of-a-map-of-pointers-to-objects-in-c – user2537246 Feb 07 '19 at 23:56
  • 1
    Can you point out exactly where you believe `itEmployeeID` gets initialized, in the shown code above, before attempts are made to dereference it? How exactly do you expect to take `itEmployeeID->second`, which is an `Employee *`, and have it automatically converted to a `std::string` for the purpose of getting pushed back into a vector of strings? Do you have a custom overload, of some kind, that somehow transforms an `Employee *` into a `std::string`? – Sam Varshavchik Feb 08 '19 at 00:14
  • added that class from that link i am following up. – user2537246 Feb 08 '19 at 00:16
  • @user2537246 Any particular reasoning why you're using a raw pointer for `_employeePayroll`? – πάντα ῥεῖ Feb 08 '19 at 00:18
  • Yes since I am referencing that specific _employeeID so I have more maps that are not only for id, like name etc. – user2537246 Feb 08 '19 at 02:04

1 Answers1

2

You might do something like:

int main() {
    std::map<std::string, Employee> employee {
       {"Karl", Employee{42}},
       {"George", Employee{59}},
    };

    for (const auto& p : employee ) {
        std::cout << p.first << std::endl; // "Karl", "George"
        // p.second is Employee object.
    }
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • how does the object gets print out for that second object? – user2537246 Feb 08 '19 at 00:13
  • @user2537246: do you mean `p.second`? You can tell your program how to output an `Employee` object - just add this function after the `Employee` class definition: `std::ostream& operator<<(std::ostream& os, const Employee& e) { return os << "{ id " << e.employeeID << " }"; }`. Then you can change Jarod's line to `std::cout << p.first << ' ' << p.second << '\n';` and it'll know to call the streaming function to output the `Employee` object. – Tony Delroy Feb 08 '19 at 02:37
  • why not use vectors if I have more than one record in that map. yes i mean p.second. – user2537246 Feb 08 '19 at 04:05
  • @user2537246: Demo added. – Jarod42 Feb 08 '19 at 09:44
  • I see how you overload the ostream, thank you for the demo, helps a lot! I have those maps inside Employee class example: std::map _employeeID , which the value is a pointer class so I access p->second->id. I still prefer dumping all map records in a vector so it doesn't print too many lines, but your demo just do maps print outs. – user2537246 Feb 08 '19 at 12:50
  • Thank you for the demo Jarod42, I will explore more into it, but understood the basics already. – user2537246 Feb 09 '19 at 03:02