str+vectors

how do i access vector data within a structure.?
eg.
struct coolstr
{
vector<int> var;
};

ofstream output_file("test1.txt");

if (!output_file.fail())
{

output_file << ??? (what would i put here to access the data in vector ?
if i use output_file << coolstr.var
i get the fol error


error: expected primary-expression before '.' token
The reason you are getting that error is because "coolstr" is a type, not an instance. You need declare it like any other variable: coolstr somecoolstr;

Anyway, an std::vector is basically an array so you use something like this to loop through the vector and get the information:

1
2
3
for(std::vector<int>::iterator i = somecoolstr.var.begin(); i != somecoolstr.var.end(); ++i) {
//do stuff with i
}
Topic archived. No new replies allowed.