user1917485
user1917485

Reputation: 85

Print struct element of vector

I am learning about vector. I try to implement a code that print the struct element of a vector as displayed below. Many resources in the internet only teach me a simple vector. I get stcuk in expression when to print it. However, any suggestion for improving the quality and elegance of the code is open, although the change is fundamental (in struct or looping).

Thank you very much.

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;

typedef struct _student {
string name;
int age;
vector <string> subject;
}student;

int _tmain(int argc, _TCHAR* argv[])
{
  vector <student> x; //assmue at this point we do not know the number of students
  student y;

  //and I want to insert new information
  y.name ="John";
  y.age =9;
  y.subject.push_back("biology");
  y.subject.push_back("math");
  y.subject.push_back("art");
  x.push_back(y);       

  //get new information again
  //and I want to insert new information
  y.name ="Bon";
  y.age =12;
  y.subject.push_back("history");
  y.subject.push_back("physics");
  x.push_back(y);       

  // then I want display all data
  cout << "myvector contains:";

  for (int i=0; i<x.size(); i++)
  {   
      cout << "Student # " << i+1 <<endl;
      cout << "   name : " << x.at(i).name <<endl;  //Reference in the internet only display a simple vector --
      cout << "   age  : " << x.at(i).age <<endl;   //I get stuck to express this and next part
      cout <<"   Subject : ";
      for (int j =0; j < x.at(i).subject.size(); j++)
      {   
          cout << x.at(i).subject.at(j);
      }
      cout << endl;
cin.get();
return 0;
}

Upvotes: 2

Views: 6147

Answers (3)

cmc
cmc

Reputation: 2091

Here, added some comments and stuff. Not sure if this is what you were looking for, but here it is.

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string> // string would be welcome here!

struct _student // the typedef thing is not necessary in C++
{
    std::string            name; // i find this "using namespace ..." thing a bad habit, it can make code harder to read
    int                            age;
    std::vector<std::string>       subject;
};

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<student>    x;
    student                 y;
    size_t                  size; // calling vector.size() every iterations is a bad idea, performance-wise
    size_t                  size_subj; // same

    y.name = "John";
    y.age = 9;
    y.subject.push_back("biology");
    y.subject.push_back("math");
    y.subject.push_back("art");
    x.push_back(y);     

    y.name = "Bon";
    y.age = 12;
    y.subject.clear(); // clear subjects of the other student
    y.subject.push_back("history");
    y.subject.push_back("physics");
    x.push_back(y);     

    std::cout << "my vector contains:";
    for (int i = 0, size = x.size(); i < size; ++i)
    {
        size_subj = x[i].subject.size();
        // I prefer using operator[] when I'm sure nothing can go wrong
        std::cout << "Student # " << i + 1 <<endl;
        std::cout << "\tname: " << x[i].name <<endl;
        std::cout << "\tage: " << x[i].age <<endl;
        std::cout << "\tSubjects: ";
        for (int j = 0; j < size_subj; ++j)   
            std::cout << x[i].subject[j];
        std::cout << endl;
    }
    return 0;
}

Finally, using a std::vector< std::string* > or std::vector< std::string& > could be a better idea performance-wise, depending on what you are planning to do with it later.

Upvotes: 2

David G
David G

Reputation: 96810

As the comments point to, it turns out that you're not including the string header file.

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129374

There is no real question here, so I'm assuming you are asking for "code review" The "neat" way is of course to create an operator<< that takes your inner structure.

Aside from that, you may want to look at using iterators to walk your way through your vector - that way, you should be able to change your vector for any other container type without having to change the loop(s) that print things.

Use longer variable names than x and y for your vector and temporary student.

Use setw to print fields at the same width every time.

I'm sure there are plenty of other suggestions too.

Upvotes: 1

Related Questions