我有一個類Atom
:複製由索引類型元件從一個矢量到另一個C++
class Atom {
public:
Atom();
string name; string mol_name; string MF; // element, molecule name, movable/frozen
int mol_id;
double m,eps,sig,C=0.0,V=0.0,K=0.0,E=0.0;
int ID;
map <string,double> pos; // equivalent of python FM1.9 "x"
map <string,double> prevpos;
map <string,double> force; // " " F
map <string,double> vel; // v
map <string,double> acc; // a
map <string,double> dip; // dipole
map <string,double> efield; // electric field;
};
並且這種原子atoms
的向量:
vector<Atom> atoms
其中填充有不同的原子( 0,1,2 ...)和相應的變量,例如name
。
我想複製,例如atoms[3]
與另一個載體tmp_atoms
。我想:
std::copy (atoms.begin() + 3, atoms.begin() + 3, tmp_atoms.begin());
無濟於事,因爲當我嘗試訪問tmp_atoms[0]
或tmp_atoms[3]
我得到一個分段錯誤。我的訪問嘗試是: printf("Make sure tmp_atoms[0] is a thing: %s \n", tmp_atoms[0].name.c_str()); // program seg. faults here
我認爲有複製類類型的特殊情況,因爲在例如整數向量的情況下這應該是正確的。請協助!
解釋downvote? – khaverim
請** [編輯] **你的問題與[mcve]或[SSCCE(Short,Self Contained,Correct Example)](http://sscce.org) – NathanOliver
這就像我能做到的那樣。目標=將一個向量元素(它本身就是一個類實例)複製到另一個向量中 – khaverim