std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::swap
来自cppreference.com
<tbody>
</tbody>
void swap( flat_map& other ) noexcept; |
(C++23 起) | |
交换容器适配器与 other 的内容。相当于调用
ranges::swap(compare, other.compare);
ranges::swap(c.keys, other.c.keys);
ranges::swap(c.values, other.c.values);
参数
| other | - | 要交换内容的容器适配器 |
返回值
(无)
异常
(无)
复杂度
与底层容器相同(常为常数)。
示例
| 本节未完成 原因:暂无示例 |
}
运行此代码
#include <iostream>
#include <concepts>
#include <flat_map>
#include <string>
#include <string_view>
#include <vector>
template<typename Adaptor>
requires (std::ranges::input_range<typename Adaptor::container_type>)
void print(std::string_view name, const Adaptor& adaptor)
{
struct Printer : Adaptor // 以使用 protected Adaptor::Container c;
{
void print(std::string_view name) const
{
std::cout << name << " [" << std::size(this->c) << "]: ";
for (auto const& elem : this->c)
std::cout << elem << ' ';
std::cout << '\n';
}
};
static_cast<Printer const&>(adaptor).print(name);
}
int main()
{
std::vector<std::string> v1{"1","2","3","4"},
v2{"Ɐ","B","Ɔ","D","Ǝ"};
std::flat_map s1(std::move(v1));
std::flat_map s2(std::move(v2));
print("s1", s1);
print("s2", s2);
s1.swap(s2);
print("s1", s1);
print("s2", s2);
}
输出:
s1 [4]: 4 3 2 1
s2 [5]: Ǝ D Ɔ B Ɐ
s1 [5]: Ǝ D Ɔ B Ɐ
s2 [4]: 4 3 2 1
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2456 | C++11 | noexcept 规定非良构
|
使之能用 |
参阅
(C++23) |
特化 std::swap 算法 (函数模板) |