简体   繁体   English

C ++ STL Vector Iterator访问Object的成员

[英]C++ STL Vector Iterator accessing members of an Object

I think I've declared a Vector with an object correctly. 我想我已经正确地声明了一个带有对象的Vector。 But, I don't know how to access it's members when looping with Iterator. 但是,在使用Iterator进行循环时,我不知道如何访问它的成员。

In my code, the line --->> cout << " " << *Iter; 在我的代码中,行--- >> cout <<“”<< * Iter;

How do I print the contents of the members? 如何打印成员的内容? Like *Iter.m_PackLine ??? 喜欢* 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. 仅当CFileInfo有一个可以输出结构的重载operator<<时才会起作用。 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. 你必须在* Iter周围加上括号,因为成员访问运算符会绑定更严格的。

On a side-node, make your main function return int instead of void. 在side-node上,使main函数返回int而不是void。 making it return void is not valid in C++. 使其返回void在C ++中无效。


You declare the vector like this: 你声明这样的向量:

vector<CFileInfo, CFileInfo&> unsortedFiles;

The second argument to vector should be another thing. vector的第二个参数应该是另一回事。 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 ). 我注意到的另一件事是你使用Iter++增加迭代器(称为postfix increment )。 For iterators, always prefer ++Iter , which is called prefix increment . 对于迭代器,总是更喜欢++Iter ,它被称为prefix increment

Use (*iter).member or iter->member. 使用(* iter).member或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. 另外,对于你正在做的事情,你可能想要一个const_iterator而不是一个(可变的)迭代器。

In addition, std::vector is a template accepting a typename and an allocator, not two typenames. 另外,std :: vector是一个接受typename和allocator的模板,而不是两个类型名。 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. main应该返回一个int。
  • It'd probably be best to declare your iterator variable in the for statement. 最好在for语句中声明你的iterator变量。
  • 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. 在for循环中使用前缀++运算符(++ iter)而不是后缀运算符(iter ++)可能会更快地运行时性能。
  • No need for your comment about main() ending. 不需要你对main()结尾的评论。

This is the first problem I noticed: 这是我注意到的第一个问题:

std::vector is a template. std::vector是一个模板。

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. 现在我考虑一下,你的模板定义可能刚刚被stackoverflow注释系统解析出来。

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. litb还指出了我在向量声明中遇到的问题。 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. Stackoverflow解析了我的一些代码,下次发布时我会更加小心。

vector<CFileInfo, CFileInfo& > will not work at all. vector<CFileInfo, CFileInfo& >根本不起作用。 The second parameter to vector is the allocator the vector uses, and CFileInfo does not meet those requirements, nor does any reference type. 向量的第二个参数是向量使用的分配器, CFileInfo不满足这些要求,也没有任何引用类型。 I think you just want vector<CFileInfo> , the iterators and members will return CFileInfo& automatically. 我认为你只需要vector<CFileInfo> ,迭代器和成员将自动返回CFileInfo&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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