对自定义元素向量进行排序时出现意外(至少对我来说)行为

Unexpected (for me at least) behaviour when sorting a vector of custom elements

本文关键字:意外 对我来说 行为 向量 元素 自定义 排序      更新时间:2023-10-16

我最近在实现某些东西时遇到了这个问题。我有一个自定义的树状结构,其中包含一个值和一个子向量。插入子节点时,我希望它们以随机顺序出现,并且我需要跟踪插入的最后一个元素以供将来的操作使用。事实证明,如果我保存指向最后一个节点向量的指针,则在对向量进行排序后,指针仍然有效,但现在它指向一个完全不同的向量。下面是一个最小示例:

#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号房子里,这不是你的朋友!

我不确定是否有任何关于此的文档,因为它是意料之中的!