std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::replace
來自 cppreference.com
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 不包含相等的元素。
否則,行為未定義。
目錄 |
[edit] 引數
keys_cont | - | 型別為 KeyContainer 的已排序鍵容器,其內容將移動到 *this |
mapped_cont | - | 型別為 MappedContainer 的對映值容器,其內容將移動到 *this |
[edit] 返回值
(無)
[edit] 複雜度
等於應用於適配容器的 std::move 的複雜度。
[edit] 示例
執行此程式碼
#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}
[edit] 參閱
提取底層容器 (公共成員函式) |