gpt4 book ai didi

c++ - 创建唯一对象对列表

转载 作者:太空狗 更新时间:2023-10-29 22:57:14 26 4
gpt4 key购买 nike

我有一个对象实例的 QVector Atom ,其中每个 Atom实例包含一组笛卡尔坐标和唯一索引标识符,以及其他属性。我还定义了一个 Dyad容器,它只是两个元组 Atom实例。基本上,如果我的两个 Atom实例满足距离约束,我希望能够构建 Dyad 的 QList秒。

假设我有一个 Dyad的 ( Atom1 , Atom2 ), 我如何确保我的 QList 不包含 ( Atom2 , Atom1 ) 的 Dyad?

我已经尝试使用 QList .contains()功能并使我的 == 重载运营商,但我无法让它工作。我可以附上我尝试使用 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;

最佳答案

我不确定我是否确切知道您的 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类:

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() 函数的返回值来检查一个项目是否被添加到集合中。

关于c++ - 创建唯一对象对列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com