c++迭代器混乱

发布于 2024-08-15 08:55:10 字数 264 浏览 2 评论 0 原文

我有一个 vector >

我有一个迭代器 vector >::const_iterator x

当我尝试像这样访问 customClass 的成员时:

x[0]->somefunc(),我收到非指针类型的错误/未找到。

I have a vector<list<customClass> >

I have an iterator vector<list<customClass> >::const_iterator x

When I try to access an member of customClass like so:

x[0]->somefunc(), I get errors of a non-pointer type/not found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

够钟 2024-08-22 08:55:10

这是一个完整的工作片段。为了回答您的问题,带有注释 [1] 的行显示了如何取消引用 const_iterator,而注释 [2] 显示了如何使用运算符 [] 取消引用。

#include <vector>
#include <list>
#include <iostream>
class Foo
{
public:
    void hello() const
    {
        std::cout << "hello - type any key to continue\n";
        getchar();
    }

    void func( std::vector<std::list<Foo> > const& vec )
    {
        std::vector<std::list<Foo> >::const_iterator qVec = vec.begin();
        qVec->front().hello(); // [1] dereference const_iterator
    }
};
int main(int argc, char* argv[])
{
    std::list<Foo>  list;
    Foo foo;
    list.push_front(foo);
    std::vector<std::list<Foo> > vec;
    vec.push_back(list);

    foo.func( vec );
    vec[0].front().hello(); // [2] dereference vector using []
}

Here's a complete working snippet. To answer your question, the line with the comment [1] shows how to dereference the const_iterator, while comment [2] shows how to dereference using the operator [].

#include <vector>
#include <list>
#include <iostream>
class Foo
{
public:
    void hello() const
    {
        std::cout << "hello - type any key to continue\n";
        getchar();
    }

    void func( std::vector<std::list<Foo> > const& vec )
    {
        std::vector<std::list<Foo> >::const_iterator qVec = vec.begin();
        qVec->front().hello(); // [1] dereference const_iterator
    }
};
int main(int argc, char* argv[])
{
    std::list<Foo>  list;
    Foo foo;
    list.push_front(foo);
    std::vector<std::list<Foo> > vec;
    vec.push_back(list);

    foo.func( vec );
    vec[0].front().hello(); // [2] dereference vector using []
}
灼疼热情 2024-08-22 08:55:10

迭代器取消引用列表。如果您想访问该列表中的对象,则必须使用列表方法来执行此操作。但是,由于 stl 列表不会重载索引运算符,因此这不是一个有效的选项。

这将允许您对列表中的第一个元素调用 somefunc:

(*x).front().somefunc();

另一方面,如果您想要列表的迭代器,您可以执行以下操作:

list<customClass>::const_iterator listIterator = (*x).begin();
listIterator->somefunc();

The iterator dereferences to a list. If you want to access an object in that list, then you will have to use the list methods to do so. However, since stl lists don't overload the index operator, that won't be a valid option.

This will let you call somefunc on the first element in the list:

(*x).front().somefunc();

On the other hand, if you want an iterator for your list, you can do something like this:

list<customClass>::const_iterator listIterator = (*x).begin();
listIterator->somefunc();
把回忆走一遍 2024-08-22 08:55:10

迭代器类不提供operator[],因此你不能这样使用它。您应该将其用作 x->somefunc()

iterator class does not provide operator[] hence you can not use it like that . You should use it as x->somefunc()

转角预定愛 2024-08-22 08:55:10

x 是一个迭代器,它的作用就像一个指针——它指向一个列表。所以你只能使用 std::list 成员的函数。

x is an iterator, which acts like a pointer - it points to a list. So you can only use functions which are members of std::list.

话少心凉 2024-08-22 08:55:10

const 迭代器将取消引用 list 对象,而不是指向该列表的指针。您必须访问该类列表中的索引...

忽略错误检查:

(*x)[0].somefunc()

The const iterator will dereference to a list<customClass> object not a pointer to that list. You'd have to access the index in that list for the class...

Ignoring error checking:

(*x)[0].somefunc()
执笔绘流年 2024-08-22 08:55:10

如果你想直接访问列表和向量类已经实现了 [] 运算符,那么只需直接访问它:vectorObj[x].someFunc();

迭代器用于遍历列表(如名称所示迭代它),请使用它。

if you want direct access the list and vector class already implement the [] operator, so just access it directly: vectorObj[x].someFunc();

iterator are for going through the list (to iterate it like the name suggests), use it for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文