Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ STL Vector Iterator accessing members of an Object

Tags:

c++

I think I've declared a Vector with an object correctly. But, I don't know how to access it's members when looping with Iterator.

In my code, the line --->> cout << " " << *Iter;

How do I print the contents of the members? Like *Iter.m_PackLine ???

Not sure if I used the correct terminology, but appreciate the help! Thanks

class CFileInfo
{
  public:
      std::string m_PackLine;
      std::string m_FileDateTime;
      int m_NumDownloads;
};

void main()
{
  CFileInfo packInfo;

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

  packInfo.m_PackLine = "Sample Line 1";
  packInfo.m_FileDateTime = "06/22/2008 04:34";
  packInfo.m_NumDownloads = 0;
  unsortedFiles.push_back(packInfo);

  packInfo.m_PackLine = "Sample Line 2";
  packInfo.m_FileDateTime = "12/05/2007 14:54";
  packInfo.m_NumDownloads = 1;
  unsortedFiles.push_back(packInfo);

 for (Iter = unsortedFiles.begin(); Iter != unsortedFiles.end(); Iter++ )
 {
    cout << " " << *Iter; // !!! THIS IS WHERE I GET STUMPED
    // How do I output values of the object members? 
 }
}  // end main
like image 378
richyz Avatar asked Nov 26 '08 20:11

richyz


People also ask

How do you access the elements of a vector iterator?

Use an iteratorvector<int>::iterator iter; An iterator is used as a pointer to iterate through a sequence such as a string or vector . The pointer can then be incremented to access the next element in the sequence. To access the value in the memory space to which the iterator is pointing, you must use * .

Are vector iterators random access?

All pointer types are also valid random-access iterators. It is to be noted that containers like vector, deque support random-access iterators.

Which STL function returns an iterator?

Iterators are generated by STL container member functions, such as begin() and end(). Some containers return iterators that support only the above operations, while others return iterators that can move forward and backward, be compared with <, and so on.


2 Answers

cout << " " << *Iter;

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

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

Alternatively, the following is equivalent to that:

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

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

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


You declare the vector like this:

vector<CFileInfo, CFileInfo&> unsortedFiles;

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

vector<CFileInfo> unsortedFiles;

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

like image 162
Johannes Schaub - litb Avatar answered Oct 19 '22 07:10

Johannes Schaub - litb


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

You can also use temporaries:

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

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

In addition, std::vector is a template accepting a typename and an allocator, not two typenames. You can use the default allocator by stripping the second 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.
like image 35
strager Avatar answered Oct 19 '22 08:10

strager