2017-07-26 119 views
3

我有一個對象實例的QVector Atom,其中每個Atom實例包含一組笛卡爾座標和唯一索引標識符以及其他屬性。我還定義了一個Dyad容器,它只是兩個Atom實例的元組。基本上,如果我的兩個Atom實例滿足距離約束,我希望能夠構造一個Dyad s的QList。創建唯一對象對的列表

說我有(Atom1Atom2)一Dyad,我怎麼能保證我的QList尚未包含的(Atom2Atom1)成對層?

我已經嘗試使用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; 
+0

嘗試使用[QPair](http://doc.qt.io/qt-5/qpair.html) – aghilpro

+0

@aghilpro我將來需要將這種方法推廣到獨特的3個原子集,爲此使用QPair將不起作用 – Ecaloota

+0

我會使用'std :: set'容器並重載'AtomDyad'類的'operator <()'。 – vahancho

回答

1

我不知道我確切地知道你的AtomAtomDyad類看起來怎麼樣,但我會模仿他們在一個簡單的例子來幫助你得到這個想法。我假設,Atom有三個座標:x,y和z。讓我們的代碼現在:

struct Atom 
{ 
    Atom(float x, float y, float z) 
    : m_x(x), m_y(y), m_z(z) 
    {} 
    float m_x; 
    float m_y; 
    float m_z; 

    // Sort first by x, than by y and finally by z coordinates. 
    bool operator<(const Atom &other) const 
    { 
    if (m_x < other.m_x) 
    { 
     return true; 
    } 
    else if (m_x == other.m_x) 
    { 
     if (m_y < other.m_y) 
     { 
     return true; 
     } 
     else if (m_y == other.m_y) 
     { 
     if (m_z < other.m_z) 
     { 
      return true; 
     } 
     } 
    } 
    return false; 
    } 

    // To compare two atoms. 
    bool operator==(const Atom &other) const 
    { 
    return m_x == other.m_x && m_y == other.m_y && m_z == other.m_z; 
    } 
}; 

現在讓我們來定義AtomeDyad類,主要包括兩個Atoms

struct AtomDyad 
{ 
    AtomDyad(const Atom &a1, const Atom &a2) 
    : m_a1(a1), m_a2(a2) 
    {} 
    Atom m_a1; 
    Atom m_a2; 

    bool operator<(const AtomDyad &other) const 
    { 
    if (m_a1 == other.m_a2 && m_a2 == other.m_a1) 
    { 
     return false; 
    } 

    if (m_a1 < other.m_a1) 
    { 
     return true; 
    } 
    else if (m_a1 == other.m_a1) 
    { 
     if (m_a2 < other.m_a2) 
     { 
     return true; 
     } 
    } 
    return false; 
    } 
}; 

最後,讓我們存儲唯一AtomDyads。單元測試:

std::set<AtomDyad> uniqueAtomDyad; 

Atom a1(0, 0, 0); 
Atom a2(0, 0, 1); 
Atom a3(0, 1, 1); 
Atom a4(1, 1, 1); 

AtomDyad ad1(a1, a2); 
AtomDyad ad2(a3, a4); 
AtomDyad ad3(a1, a2); // same as ad1 
AtomDyad ad4(a4, a3); // swapped ad3 
AtomDyad ad5(a1, a1); 
AtomDyad ad6(a1, a1); 

uniqueAtomDyad.insert(ad1); 
uniqueAtomDyad.insert(ad2); 
uniqueAtomDyad.insert(ad3); // not unique 
uniqueAtomDyad.insert(ad4); // not unique 
uniqueAtomDyad.insert(ad5); 
uniqueAtomDyad.insert(ad6); // not unique 

assert(uniqueAtomDyad.size() == 3); 

您可以檢查項目是否通過檢查std::set::insert()函數的返回值添加到集合。

+0

使用你的例子我能夠引導我的代碼來實現我所需要的,謝謝。 – Ecaloota