std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::replace
来自cppreference.com
<tbody>
</tbody>
void replace( key_container_type&& key_cont, mapped_container_type&& mapped_cont ); |
(C++23 起) | |
替换底层容器 c。等价于:
c.keys = std::move(key_cont);
c.values = std::move(mapped_cont);
以下各条件必须满足:
- 表达式
key_cont.size() == mapped_cont.size()为true, key_cont的各元素根据compare有序,且key_cont不包含相等元素。否则,其行为未定义。
参数
| keys_cont | - | KeyContainer 类型的有序的键容器,其内容将被移动给 *this
|
| mapped_cont | - | MappedContainer 类型的被映射值的容器,其内容将被移动给 *this
|
返回值
(无)
复杂度
等于对所适配的各容器应用的 std::move 的复杂度。
示例
运行此代码
#include <algorithm>
#include <cassert>
#include <flat_map>
#include <print>
#include <vector>
int main()
{
std::vector<int> keys{1, 2, 3};
assert(std::ranges::is_sorted(keys));
std::vector<double> values{2.2, 3.3, 1.1};
assert(keys.size() == values.size());
std::flat_map<int, double> map;
assert(map.empty());
map.replace(keys, values);
assert(map.size() == 3);
assert(map.keys() == 3);
assert(map.values() == 3);
assert(keys.empty());
assert(values.empty());
std::println("{}", map);
}
输出:
{1: 2.2, 2: 3.3, 3: 1.1}
参阅
| 提取底层容器 (公开成员函数) |