【发布时间】:2018-01-17 14:23:14
【问题描述】:
所以如果我有以下向量,其中有几个指向整数的指针:
std::vector<MyClass*> list;
我以后可以使用类似的方法对其进行迭代吗:
for (auto & (*item) : list)
{
item.member = something; //Goal is to require no dereferencing here
}
使用对列表内容值的引用比使用指向它们的指针更方便。
【问题讨论】:
-
您可能可以编写和适配器,但我怀疑这会更糟
-
你也可以看看boost pointer container library ...
-
出于好奇。
item->member有什么不好? -
It would just be slightly more convenient to work with references to the list's contents' values than with pointers to them.是迂腐的,从技术上讲,指针是列表内容的值。 -
@StoryTeller 我认为 OP 没有正确地提出问题。例如,当容器保存指向函子的指针时,它可能很有用。所以你不必写
(*pitem)();
标签: c++ pointers reference iterator auto