std::flat_m...map::replace(3)
okay so this is the std::flat_m...map::replace(3) page β folded in from the C++ Standard Libary manual β and bestie, we are about to make replacing your underlying containers feel like a main-character move. π
β¨
πΈ NAME πΈ
std::flat_multimap::replace β that's the name, and the name is std::flat_multimap::replace. No plot twist, just a member function ready to swap your underlying containers. π
β¨ Synopsis β¨
Here's the whole vibe β this one is since C++23:
void replace( key_container_type&& key_cont, mapped_container_type&& mapped_cont );
It replaces the underlying containers c. Equivalent to:
c.keys = std::move(key_cont);
c.values = std::move(mapped_cont);
The following conditions must be met:
- The expression
key_cont.size() == mapped_cont.size()is true, - The elements of
key_contare sorted with respect tocompare.
π¨ Otherwise, the behavior is undefined. Not "maybe fine", not "we'll see" β undefined. Protect your peace and your program, bestie.
π Parameters π
keys_contβ a sorted keys container of typeKeyContainer; its contents will be moved into*this. π«Άmapped_contβ a container of mapped values of typeMappedContainer; its contents will be moved into*this. π
π¦ Return value π¦
(none). That's it. No return, no drama. π
π₯ Complexity π₯
Equals to complexity of std::move applied to adapted containers. That's the whole sentence β we're not adding lore. π
π Example π
// Run this code
#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_multimap<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);
}
π Output π
{1: 2.2, 2: 3.3, 3: 1.1}
π See also π
extractβ extracts the underlying containers (public member function). π₯
cute sign-off: source/version is http://cppreference.com, date is 2024.06.10, page is std::flat_m...map::replace(3). mwah ππ«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms