[ACCEPTED]-C++ STL Vector Iterator accessing members of an Object-c++

Accepted answer
Score: 17
cout << " " << *Iter;

will only work if CFileInfo has an overloaded operator<< that 15 can output your struct. You can output individual 14 members of the struct instead like this:

cout << " " << Iter->m_PackLine;

Alternatively, the 13 following is equivalent to that:

cout << " " << (*Iter).m_PackLine;

You have 12 to put parentheses around *Iter, since the 11 member-access operator binds thighter otherwise.

On 10 a side-node, make your main function return 9 int instead of void. making it return void 8 is not valid in C++.

You declare the vector 7 like this:

vector<CFileInfo, CFileInfo&> unsortedFiles;

The second argument to vector should 6 be another thing. It's not needed for your 5 code to give the vector a second argument 4 at all. Just use this:

vector<CFileInfo> unsortedFiles;

Another thing i noticed 3 is you increment the iterator using Iter++ (called 2 postfix increment). For iterators, always prefer ++Iter, which 1 is called prefix increment.

Score: 3

Use (*iter).member or iter->member.

You can 7 also use temporaries:

CFileInfo &fileInfo = *iter;
cout << " " << fileInfo.myMember;

Also, for what you're 6 doing, you'd probably want a const_iterator 5 instead of an (mutable) iterator.

In addition, std::vector 4 is a template accepting a typename and an 3 allocator, not two typenames. You can use 2 the default allocator by stripping the second 1 template argument:

vector<CFileInfo> unsortedFiles;
vector<CFileInfo>::iterator Iter;

Some nit-picking:

  • main should return an int.
  • It'd probably be best to declare your iterator variable in the for statement.
  • It'd probably be faster in run-time performance to use the prefix ++ operator (++iter) instead of the postfix operator (iter++) in your for loop.
  • No need for your comment about main() ending.
Score: 1

This is the first problem I noticed:

std::vector is 4 a template.

You have:

vector unsortedFiles;

you need something like:

vector<CFileInfo> unsortedFiles;

Now 3 that I think about it, your template definition 2 may have just gotten parsed out by the stackoverflow 1 comment system.

Score: 1

First correct you'r vector declaration:

vector<CFileInfo > unsortedFiles;

Next 9 you need to define an output operator for 8 your class:

std::ostream& operator<<(std::ostream& str,CFileInfo const& data)
       // Do something here
       /* Potentailly you could do this
        *    But this requires that this function be a friend of the class

       str << data.m_PackLine << ":"
           << data.m_FileDateTime << ":"
           << data.m_NumDownloads << ":";

       *  Or you could do this

           data.print(str);  // Make print a public const method.


       return str;

Usually you either make the output 7 operator a friend of your class or provide 6 a public print method that takes a stream. Either 5 way you can then access the members and 4 stream them manually to the output.

Once 3 you have the output iterator defined you 2 can change your loop to use the standard 1 library versions:

              std::ostream_iterator<CFileInfo>(std::cout," ")
Score: 1




Score: 0

Thanks all, wish I could grant multiple 6 points for the answers :)

litb also pointed 5 out a problem I was having in my declaration 4 of the vector. I removed the second argument 3 in the vector declaration and it worked.

Stackoverflow 2 parsed out some of my code, I'll be more 1 careful in posting next time.

Score: 0

vector<CFileInfo, CFileInfo&> will not work at all. The second parameter 5 to vector is the allocator the vector uses, and 4 CFileInfo does not meet those requirements, nor does 3 any reference type. I think you just want 2 vector<CFileInfo>, the iterators and members will return 1 CFileInfo& automatically.

More Related questions