C++ 如何遍历 std::list<MyClass *> 并从迭代器获取该类中的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1445736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:59:36  来源:igfitidea点击:

How do I iterate through a std::list<MyClass *> and get to the methods in that class from the iterator?

c++

提问by Loren C Fortner

If I have the list,

如果我有名单,

typedef std::list<MyClass *> listMyClass;

How do I iterate through them and get to the methods in that class?

如何遍历它们并访问该类中的方法?

This is what I've tried, but that is not it: (MyClass::PrintMeOut() is a public method)

这是我尝试过的,但事实并非如此:(MyClass::PrintMeOut() 是一个公共方法)

for(
    listMyClass::iterator listMyClassIter = listMyClass.begin();
    listMyClassIter != listMyClass.end();
    listMyClassIter ++)
{
    listMyClassIter->PrintMeOut();  
}     

回答by Phil Miller

Use this method:

使用这个方法:

(*listMyClassIter)->PrintMeOut();

回答by Martin York

for_each and function pointer:

for_each 和函数指针:

    std::for_each(  listMyClass.begin(),
                    listMyClass.end(),
                    std::mem_fun(&MyClass::PrintMeOut)
                 );

I prefer using the for_each() construct rather than writting my own loop.
It makes it look neat and does all the things I would otherwise need extra code for.

我更喜欢使用 for_each() 构造而不是编写自己的循环。
它使它看起来很整洁,并且可以完成我需要额外代码才能完成的所有事情。

  1. Only calling end() once rather than each iteration.
  2. Remembering to use pre increment rather than post increment.
  3. Needing to work out the actual types of my iterators.
  4. I am sure there are more but its early in the morning here.
  1. 只调用 end() 一次而不是每次迭代。
  2. 记住使用前增量而不是后增量。
  3. 需要计算出我的迭代器的实际类型。
  4. 我相信还有更多,但这里是清晨。

回答by Kirill V. Lyadvinsky

typedef std::list<MyClass*> listMyClass;
listMyClass instance; // instance, you shouldn't use type in the following loop

for( listMyClass::iterator listMyClassIter = instance.begin(); // not listMyClass.begin()
    listMyClassIter != instance.end(); // not listMyClass.end()
    listMyClassIter ++)
{
    (*listMyClassIter)->PrintMeOut();  
}

回答by Rick

std::for_each in is incredibly well suited for this, some compilers are now picking up lambdas from C++0x which makes this even more intuitive.

std::for_each in 非常适合这一点,一些编译器现在从 C++0x 中提取 lambdas,这使得这更加直观。

typedef std::list<MyClass*> MyClassList;
MyClassList l;
for_each(l.begin(),l.end(),[](MyClass* cur)
{
   cur->PrintMeOut();
});

for_each (and the rest of the algorithms) help mask the abstraction between the iterators and types. Also note that now I have this little tiny lambda function (or it could be a functor too) which is more testable, mockable, replacable etc.

for_each(以及其他算法)有助于掩盖迭代器和类型之间的抽象。还要注意,现在我有这个小小的 lambda 函数(或者它也可以是一个函子),它更可测试、可模拟、可替换等。

If I go back to notusing lambdas I can build a stand along method to do this, which is testable:

如果我回到使用 lambdas 的状态,我可以构建一个独立的方法来做到这一点,这是可测试的:

void PrintMyClass(MyClass* cur)
{
    cur->PrintMeOut();
}

and the for_each code now looks like this:

现在 for_each 代码如下所示:

typedef std::list<MyClass*> MyClassList;
MyClassList l;
for_each(l.begin(),l.end(),&PrintMyClass);