- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个对象实例的 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;
最佳答案
我不确定我是否确切知道您的 Atom
和 AtomDyad
类的外观,但我将在一个简单的示例中模拟它们以帮助您理解。我假设 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/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!