C++ STL:带指针的列表 - 迭代器无法访问?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6454281/
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-28 20:10:51  来源:igfitidea点击:

C++ STL: list with Pointers - Iterator cannot access?

c++liststl

提问by Stefan

I am struggeling with an STL list that holds Pointers of my "Object" object.

我正在努力处理一个包含我的“对象”对象指针的 STL 列表。

I declared:

我声明:

list<Object*> objectlist;

and inserted via:

并通过以下方式插入:

this->objectlist.push_back(new Object(address,value,profit));

and tried to iterate like in maps and others:

并尝试像在地图和其他人中一样进行迭代:

list<Object*>::iterator iter;

iter = this->objectlist.begin();

while(iter != this->objectlist.end())
{
    iter->print();
}

Where print() is a public Method of class Object;

其中 print() 是类 Object 的公共方法;

Whats wrong here?

这里有什么问题?

I cannot access via iterator to the objects in the list ?

我无法通过迭代器访问列表中的对象?

回答by RC.

You need (*iter)->print();

你需要 (*iter)->print();

Since you have an iterator to a pointer, you have to first de-reference the iterator (which gets you the Object*) then the arrow de-references the Object *and allows the call to print.

由于您有一个指向指针的迭代器,因此您必须首先取消引用迭代器(它为您提供Object*),然后箭头取消引用Object *并允许调用打印。

回答by Kerrek SB

You are not incrementing your iterator! Change your whileloop to a forloop like so:

你没有增加你的迭代器!将while循环更改为如下for循环:

for (list<Object*>::const_iterator iter = this->objectlist.begin(),
     end = this->objectlist.end();
     iter != end;
     ++iter)
{
    (*iter)->print();
}

(Also iterdereferencesto a pointer, like the other answers have pointed out.)

(也iter取消对指针的引用,就像其他答案指出的那样。)

回答by Juho

You can access the value pointed by iterator with *iter

您可以使用 *iter 访问迭代器指向的值

Also, remember to increment the iterator in each iteration. Otherwise you get stuck in an endless loop.

另外,请记住在每次迭代中递增迭代器。否则你会陷入无限循环。

Like this:

像这样:

iter = this->objectlist.begin();

while(iter != this->objectlist.end())
{
    (*iter)->print();
    iter++;
}

回答by alanw

You could try the C++11 way (if possible). The dereference of the iterator comes free :)

您可以尝试 C++11 方式(如果可能)。迭代器的取消引用是免费的:)

for (auto& iter: this->objectlist)
{
     iter->print();
}

Also if print() is a const function you should use

此外,如果 print() 是一个 const 函数,您应该使用

for (const auto& iter: this->objectlist)

回答by ollo

Instead of the for-loop you should use STL's for_each()- see the advantages of for_eachover a simple for-loop.

for您应该使用 STL而不是-loop for_each()- 查看for_each相对于简单for-loop的优势

Syntax

句法

#include <algorithm> // 'for_each()' is in there

std::for_each(InputIterator first, // Iterator start position
              InputIterator last,  // Iterator end position
              Function fn);        // Unary function - executed on all elements in range

for_eachexample

for_each例子

Here's how your example looks std::for_each()instead of for( ... ):

这是您的示例的外观std::for_each()而不是for( ... )

list<Object*> objectlist;

// insert some elements …

// Deletes all elements of 'objectlist'
std::for_each(objectlist.begin(), objectlist.end(), DeleteObj<Object*>());

Unary function

一元函数

The unary function is implemented using a template, so you can use it with anytype. Just make sure Tis a pointer type!

一元函数是使用模板实现的,因此您可以将它用于任何类型。只要确保T是指针类型

template<class T> class DeleteObj
{
public:
    bool operator()(T obj) const
    {
        delete obj;
        return true;
    }
};

Btw. you don't haveto implement this as template. And: you can binda member function instead of such a implementation too!

顺便提一句。您不必将此实现为模板。并且:您也可以绑定成员函数而不是这样的实现!

Documentation

文档

回答by Frerich Raabe

An iterator references an element in the list, so for a list of type T, dereferencing an iterator yields a value of type T.

迭代器引用列表中的一个元素,因此对于 type 的列表T,取消引用迭代器会产生一个 type 的值T

In your case, Tis actually a pointer - Object*. Hence, the iter->call (which dereferences the iterator) yields a pointer. You have to dereference that pointer to get to the actuala object.

在您的情况下,T实际上是一个指针 - Object*。因此,iter->调用(取消对迭代器的引用)产生一个指针。您必须取消引用该指针才能到达实际对象。

Try using

尝试使用

(*iter)->print()

instead.

反而。

回答by u804609

you should use the following way:

您应该使用以下方式:

(*iter)->print()