std::set 插入后元素的引用和指针不变性

std::set elements' reference and pointer invariance after insertion

我在 cppreference 上看到了一段我不明白的内容:

No iterators or references are invalidated. [If the insertion is successful, pointers and references to the element obtained while it is held in the node handle are invalidated, and pointers and references obtained to that element before it was extracted become valid. (since C++17)]

我希望 No iterators or references 到集合中已经存在的对象 are invalidated 意味着以下是正确的:

    std::set<person> set{person{"Kate", "Oceanic"}};
    const auto &kate = *set.cbegin();

    std::cout << kate.flight << std::endl;

    // this does not invalidate reference to Kate
    set.insert(person{"John", "Oceanic815"});
    std::cout << kate.flight << std::endl;

但我不明白 C++17 部分的含义:...pointers and references to the element obtained while it is held in the node handle are invalidated, ... node handle 是否引用 iterator?
... and pointers and references obtained to that element before it was extracted become valid 这里 become 到底是什么意思?

完整example:


#include <set>
#include <string>
#include <iostream>

struct person
{
    std::string name;
    std::string flight;

    bool operator<(const person& other) const
    {
        return name < other.name;
    }
};

int main()
{
    std::set<person> set{person{"Kate", "Oceanic"}};
    const auto &kate = *set.cbegin();

    std::cout << kate.flight << std::endl;

    // this does not invalidate Kate
    set.insert(person{"John", "Oceanic815"});
    std::cout << kate.flight << std::endl;

    // is new Kate created within the same address?
    set.insert(person{"Kate", "Oceanic815"});
    std::cout << kate.flight << std::endl;

    return 0;
}

Does node handle refer to an iterator?

没有

C++17 中引入的“节点句柄”指的是 std::set 内部的数据结构,用于保存单个元素。这是 std::set 将从空闲存储中分配的内容,用于保存元素和任何相关数据结构,例如指针和标志。

此结构的句柄允许从集合中删除元素(通过 std::set::extract)而不触发内存重新分配,以及添加到集合中而不触发内存分配。

and pointers and references obtained to that element before it was extracted become valid

这里 become 到底是什么意思?

表示在节点句柄中对某个元素的指针和引用在提取时是无效的,但是一次有效再次将相同的 节点句柄 插入到集合中。

A node-handle 不是迭代器。 node-handle 是当您调用 extractset 中删除一系列节点时得到的东西(比方说)。有关更多信息,请参阅 cppreference