我有一类粒子像这样,<Class *>没有得到复制构造的std ::矢量正确
class Particle
{
public:
std::vector<Particle*> getNbh() const;
void setNbh (const std::vector<Particle*>&);
private:
std::vector<Particle*> nbh_;
};
Particle::setNbh(const std::vector<Particle*>&)
和实现的功能,
void Particle::setNbh (const std::vector<Particle*>& nbh)
{
nbh_ = nbh;
}
再有一个非成员功能updateNeighbors (std::vector<Particle>& particles, double cutoff)
void updateNeighbors (std::vector<Particle>& particles, double cutoff)
{
for (auto particle : particles)
{
auto nbh = std::vector<Particle*>();
for (auto other : particles)
if (&particle != &other
&& norm(particle.getPosition() - other.getPosition()) < cutoff)
nbh.push_back(&other);
particle.setNbh(nbh);
}
}
事情是,当我更新邻居的机智h此功能,nbh_
成员没有正确更新,我测试它打印每个粒子的大小getNbh()
。
哪个是复制构建的正确方法std::vector<Particle*>
所以我可以得到想要的行为?
整个执行[这里](http://dpaste.org/voLsx/) – 2013-04-08 04:18:39
nbh_是指针粒子和updateNeighbors的矢量由值取粒子的载体。它将如何工作? updateNeighbors如何访问nbh_? – 2013-04-08 04:20:10
我把它作为参考,它的目的是更新粒子的向量中的每个粒子的nbh_ – 2013-04-08 04:23:20