std::flat_map::replace(3)
π std::flat_map::replace(3) π
okay so bestie, welcome to the C++ Standard Libary page for std::flat_map::replace β yes the manual name is literally "C++ Standard Libary" (not a typo, that's just how it's spelled in the header, iykyk). this one's a member function that lets you swap out the whole underlying container situation in one move. no cap, it's giving main character energy for flat_map. β¨
π NAME π
std::flat_map::replace β literally just std::flat_map::replace. that's the name. periodt. π
β¨ Synopsis β¨
the squad's signature, since C++23, fr fr:
void replace( key_container_type&& key_cont, mapped_container_type&& mapped_cont );
so what does it do? it replaces the underlying containers c. equivalent to this little move-move situation:
c.keys = std::move(key_cont);
c.values = std::move(mapped_cont);
but like, you gotta meet these conditions or it's a whole mess:
- the expression
key_cont.size() == mapped_cont.size()istrue(sizes gotta match, no cap) - the elements of
key_contare sorted with respect tocompare(sorted, bestie, sorted) key_contdoes not contain equal elements (no duplicates, we don't do that here)
β οΈ Otherwise, the behavior is undefined. like, undefined undefined. don't play with this. π
π Parameters π
keys_contβ a sorted keys container of typeKeyContainer, the contents of which will be moved into*this. it's the key VIP, literally. πmapped_contβ a container of mapped values of typeMappedContainer, the contents of which will be moved into*this. the plus-one to the key container, they're a package deal. π«Ά
πΈ Return value πΈ
(none). it's void, bestie. it does the thing and dips. no return, no drama. π
π¦ Complexity π¦
equals to complexity of std::move applied to adapted containers. that's it. that's the tea. β
π₯ Example π₯
run this code, it's iconic:
#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);
}
Output:
{1: 2.2, 2: 3.3, 3: 1.1}
and that's on periodt. the map ate and left no crumbs. π π₯
π See also π
extractβ extracts the underlying containers (public member function). it's the sibling function, also a vibe. go check it out. π
okay bestie, that's the whole page. we got the header info ("C++ Standard Libary" manual, the source is http://cppreference.com, version 2024.06.10, date 2024.06.10, and the page is std::flat_map::replace(3)). now go slay your flat_map containers. 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