对自定义元素向量进行排序时出现意外(至少对我来说)行为
Unexpected (for me at least) behaviour when sorting a vector of custom elements
我最近在实现某些东西时遇到了这个问题。我有一个自定义的树状结构,其中包含一个值和一个子向量。插入子节点时,我希望它们以随机顺序出现,并且我需要跟踪插入的最后一个元素以供将来的操作使用。事实证明,如果我保存指向最后一个节点向量的指针,则在对向量进行排序后,指针仍然有效,但现在它指向一个完全不同的向量。下面是一个最小示例:
#include <iostream>
#include <vector>
#include <algorithm>
struct Node {
int value;
std::vector<Node> nxt;
bool operator<(const Node& other) {
return value < other.value;
}
/* Having this custom swap function doesn't make a difference
*friend void swap(Node& lhs, Node& rhs) {
* std::swap(lhs.value, rhs.value);
* lhs.nxt.swap(rhs.nxt);
*}
*/
};
int main() {
Node node1;
node1.value = 1;
Node node2;
node2.value = 2;
Node node3;
node3.value = 3;
Node node4;
node4.value = 4;
std::vector<Node> container;
container.push_back(node2);
container.push_back(node1);
container.push_back(node4);
container.push_back(node3);
std::vector<Node>* node3_vec = &container.back().nxt;
node3_vec->push_back(node1);
std::cout << "Address of the vector: " << node3_vec << std::endl;
std::cout << "Size of the vector: " << node3_vec->size() << std::endl;
std::sort(container.begin(), container.end());
std::cout << "Address of the vector post sort: " << node3_vec << std::endl;
std::cout << "Size of the vector post sort: " << node3_vec->size() << std::endl;
//Inside the container
std::cout << "Value of the node inside the container: " << container[2].value << std::endl;
std::cout << "Address of the vector: " << &container[2].nxt << std::endl;
std::cout << "Size of the vector: " << container[2].nxt.size() << std::endl;
return 0;
}
我尝试使用一些自定义std::swap
实现,但我似乎无法改变这种行为。如何使排序后指向向量的指针指向同一向量?目前,我在排序后执行额外的搜索以找到所需的元素。
还有人可以指出我一些解释这种行为的文档吗?
sort
之后,原始指针node3_vec
仍将指向 container
中的最后一个Node
。在sort
之后,这将是node4
的副本,其中node3
的副本曾经在排序之前。
每当保存指针时,您都会将地址保存在下一个向量的内存中。如果对容器进行排序,元素将四处移动,另一个元素将最终到达该地址。
就像你记得你的朋友住在街上的4号房子里一样。然后你要求街上的每个人都搬家,以便他们有一定的顺序。
很可能其他人会住在4号房子里,这不是你的朋友!
我不确定是否有任何关于此的文档,因为它是意料之中的!
相关文章:
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 对自定义元素向量进行排序时出现意外(至少对我来说)行为
- 这对我来说真的很难,我该怎么办
- mod 功能对我来说无法正常工作
- 在这种情况下,为什么Sfinae对我来说不正确以及如何修复它
- 使用嵌套环以简单(对我来说很难)输出
- 是否可以安全地假设64位指针中的16个高位(2个对我来说足够了)是未设置的
- 无符号和有符号int(对我来说)之间的比较似乎是必需的(c++)
- 第4章 斯特劳斯特鲁普钻头一个具有挑战性的一步(至少对我来说!
- libc ++is_copy_constructible对我来说似乎是错误的
- 对我来说,使 boost::statechart::state_machine 线程安全的最简单方法是什么?
- 未定义的引用错误对我来说没有任何意义
- 类函数在工作时被击中是错过的,即使它们是相同的(对我来说)
- default_random_engine对我来说不是随机值
- 提升异步操作不起作用(对我来说)
- SEG在非常复杂的(对我来说)程序中出错
- 为什么我在Codechef的褪色回文中得到一个WA,即使我在我的代码中没有发现任何错误,它对我来说工作得很好
- 洛基的长寿功能对我来说似乎不安全,是这样吗?
- Q设置对我来说效果不佳
- CUDAMemcpy对我来说毫无意义…为什么要在普通c++中指定设备内存?