jeudi 7 février 2019

How to print out key and values from a map as a list in C++

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.

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();
  if ( 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.

Aucun commentaire:

Enregistrer un commentaire