gpt4 book ai didi

c++ - 在 C++ 中交换映射的键和值

转载 作者:可可西里 更新时间:2023-11-01 18:15:11 26 4
gpt4 key购买 nike

我正在寻找 C++ 中的一个函数,用于交换 map 的内容 ...那是:那些曾经是 key 的现在变成了元素,那些元素现在变成了 key 。你能告诉我这是否有什么问题吗?

最佳答案

正如 Geoffroy 所说,std::map 不允许这种行为。但是,您可能想要使用类似 STL 的容器 Boost.Bimap - 双向映射。

A Bimap is a data structure that represents bidirectional relations between elements of two collections. The container is designed to work as two opposed STL maps. A bimap between a collection X and a collection Y can be viewed as a map from X to Y (this view will be called the left map view) or as a map from Y to X (known as the right map view).

关于c++ - 在 C++ 中交换映射的键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7868217/

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