I know that I am doing something wrong, I need help. There is a structure of type Classmates_Info in it two vectors of type string, when I try to infer values ​​with the help of a loop, nothing happens that I thought up wrong? Compiler VS 2015.

for(vector<Classmates_Info>::const_iterator iter = Class.begin(); iter != Class.end(); ++iter){ cout << (*iter).names << setw(maxlenNam) << (*iter).surnames << endl; } struct Classmates_Info{ std::vector<std::string> names; std::vector<std::string> surnames; }; 
  • code code structure. - pavel
  • and the vector is it possible to directly go to cout? - Grundy
  • So I ask, how then? - STC
  • obviously the same as for a vector with Classmates_Info , why for one vector you use for and others try directly? - Grundy
  • @StirolCraft write code ostream& operator<<(ostream &out, const vector<std::string> &names) - andy.37

1 answer 1

Error here:

 cout << (*iter).names <<... 

Write it like this (it’s the same)

 for (...) { // самый внешний цикл vector<string> names = (*iter).names; cout << names; // выдаст ту же ошибку } 

You need something like this:

 for (...) { // внешний цикл for (string name: (*iter).names) { cout << name << ' '; } cout << endl; for (string name: (*iter).surnames) { cout << name << ' '; } } 

And better to write the definition of the operator

 ostream &operator <<(ostream &out, const Classmates_Info &info) { // форматируйте как Вам удобно for (string name: info.names) { cout << name << ' '; } cout << endl; for (string name: info.surnames) { cout << name << ' '; } } 

and when you need to display the vector of objects Classmates_Info

 for (const auto &info: vector<Classmates_Info>) { // Приблизительно эквивалентно Вашему for cout << info << endl; } 
  • Why, then, the author of the book does not tell me that I should use it this way, or I misunderstand something, here is an example of pp.vk.me/c636126/v636126597/160bb/1uTPOMifnjc.jpg , but I haven’t been able to compile any of them ? - STC
  • @StirolCraft I suspect that the author of the book has the Student_info structure Student_info describes ONE student and Student_info::name is of type std::string , and not at all std::vector<std::string> - andy.37