简体   繁体   中英

C++ STL Vector Iterator accessing members of an Object

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
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 .

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.

This is the first problem I noticed:

std::vector is a template.

You have:

vector unsortedFiles;

you need something like:

vector<CFileInfo> unsortedFiles;

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

First correct you'r vector declaration:

vector<CFileInfo > unsortedFiles;

Next you need to define an output operator for 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 operator a friend of your class or provide a public print method that takes a stream. Either way you can then access the members and stream them manually to the output.

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

std::for_each(unsortedFiles.begin()
              unsortedFiles.end()
              std::ostream_iterator<CFileInfo>(std::cout," ")
             );
iter->m_PackLine

要么

(*iter).m_PackLine

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM