C ++ STL Vector Iterator访问对象的成员 [英] C++ STL Vector Iterator accessing members of an Object

查看:207
本文介绍了C ++ STL Vector Iterator访问对象的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我已经正确地声明了一个Vector对象。但是,我不知道如何在使用Iterator循环时访问它的成员。



在我的代码中,行--- >> cout< < * Iter;



如何打印会员的内容? Like * Iter.m_PackLine ???



不知道我是否使用了正确的术语,但非常感谢您的帮助!感谢

  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; // !!!这是我在哪里获得
//如何输出对象成员的值?
}
} // end main


解决方案

  cout<< < * Iter; 

只有在 CFileInfo 运算符<< ,可以输出您的结构。您可以输出结构的个别成员,如下所示:

  cout< < Iter-> m_PackLine; 

或者,以下等效于:

  cout<< < (* Iter).m_PackLine; 

你必须在* Iter附加括号,因为成员访问操作符以其他方式绑定。 p>

在边结点上,使你的main函数返回int而不是void。使它返回void在C ++中无效。






  vector< CFileInfo,CFileInfo&> unsortedFiles; 

向量的第二个参数应该是另一个事情。它不需要你的代码给矢量第二个参数。只需使用:

 向量< CFileInfo> unsortedFiles; 




我注意到你是使用 Iter ++ (称为 postfix增量)增加迭代器。对于迭代器,总是喜欢 ++ Iter ,它被称为前缀增量


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.

这篇关于C ++ STL Vector Iterator访问对象的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆