我有一个类,产生任意数量的工人对象,计算他们的结果到std::vector
。我将在某些点删除一些工作对象,但我想保留他们的结果,只按照spawned
他们知道的那个类别。因此,我为类A中的输出提供了向量。我有(IMO)三个选项:我可以指向矢量,引用或迭代器作为成员。虽然迭代器选项有一定的退步(迭代器可以递增)。我不确定指针或引用是否更清晰。 我觉得引用更好,因为它们不能为NULL,并且要求存在一个向量。迭代器与参考与指针
我最不确定的是参考文献的有效性。 std::list< std::vector<int> >
上的某些操作会使其失效吗?这些操作是否与使std::list
的迭代器无效相同?有没有另外一种方法我现在没有看到?此外,与容器的耦合感觉不对:我强制将一个特定的容器加入到Cruncher类中。提供为清楚起见
代码:
#include <list>
#include <vector>
#include <boost/ptr_container/ptr_list.hpp>
class Cruncher {
std::vector<int>* numPointer;
std::vector<int>& numRef;
std::list< std::vector<int> >::iterator numIterator;
public:
Cruncher(std::vector<int>*);
Cruncher(std::vector<int>&);
Cruncher(std::list< std::vector<int> >::iterator);
};
class A {
std::list< std::vector<int> > container;
boost::ptr_list< std::vector<int> > container2;
std::vector<Cruncher> cruncherList;
};
我打算使用std :: list来存储指向对象的指针样品。如果您只是将标准的报价更改为解释列表失效的部分,则您的答案没问题。 – pmr
@pmr - 更新了我的答案,以包含更多关于列表的信息。 –