我有一個對象實例的QVector Atom
,其中每個Atom
實例包含一組笛卡爾座標和唯一索引標識符以及其他屬性。我還定義了一個Dyad
容器,它只是兩個Atom
實例的元組。基本上,如果我的兩個Atom
實例滿足距離約束,我希望能夠構造一個Dyad
s的QList。創建唯一對象對的列表
說我有(Atom1
,Atom2
)一Dyad
,我怎麼能保證我的QList尚未包含的(Atom2
,Atom1
)成對層?
我已經嘗試使用QList .contains()
函數並重載了我的==
運算符,但我無法使其工作。我可以附加我嘗試使用函數的代碼,如果這樣做會有所幫助的話。
//函數定義
QList<AtomDyad> getUniqueAtomPairs(QVector<Atom> atomVector) {
QList<AtomDyad> AtomDyadPairList;
for (int iatom = 0; iatom < atomVector.size(); iatom++) {
for (int jatom = 0; jatom < atomVector.size(); jatom++) {
if (iatom == jatom) { continue; }
float x1 = atomVector[jatom].getX();
float x2 = atomVector[iatom].getX();
float y1 = atomVector[jatom].getY();
float y2 = atomVector[iatom].getY();
float z1 = atomVector[jatom].getZ();
float z2 = atomVector[iatom].getZ();
float Radii_Sum1 = atomVector[jatom].getvdW_radius(atomVector[jatom]) + atomVector[iatom].getvdW_radius(atomVector[iatom]);
if (DistanceBetween3DPoints(x1, x2, y1, y2, z1, z2) <= Radii_Sum1) {
AtomDyad MyDyad(atomVector[iatom], atomVector[jatom]);
// How can I ensure that MyDyad(atomVector[jatom], atomVector[iatom]) does not already exist?
AtomDyadPairList.append(MyDyad);
}
}
}
return AtomDyadPairList;
嘗試使用[QPair](http://doc.qt.io/qt-5/qpair.html) – aghilpro
@aghilpro我將來需要將這種方法推廣到獨特的3個原子集,爲此使用QPair將不起作用 – Ecaloota
我會使用'std :: set'容器並重載'AtomDyad'類的'operator <()'。 – vahancho