std::map::insert_range(3)
โจ std::map::insert_range โจ
okay bestie, welcome to the std::map::insert_range(3) page from the C++ Standard Libary manual โ yes, the header really spells it like that, and honestly? iconic. this is the C++23 glow-up where a std::map gets a whole range inserted into it, and we're gonna read every crumb together. ๐
๐ NAME ๐
std::map::insert_range - std::map::insert_range. that's it, that's the NAME line, and the page is serving consistency, no cap. โจ
๐ธ Synopsis ๐ธ
template< container-compatible-range<value_type> R > (since C++23)
void insert_range( R&& rg );
Inserts a copy of each element in the range rg if and only if there is no element with key equivalent to the key of that element in *this. each iterator in the range rg is dereferenced exactly once. the behavior is undefined if rg overlaps with the container, so do NOT let them touch like that. ๐ญ no iterators or references are invalidated. periodt. ๐ซถ
๐ Parameters ๐
rgโ a container compatible range, that is, aninput_rangewhose elements are convertible toT. that's the whole parameter, bestie.
๐ Type requirements ๐
value_typemust beEmplaceConstructibleintomapfrom*ranges::begin(rg). otherwise, the behavior is undefined. do not test fate on this one. ๐ญ
๐ Return value ๐
(none) โ she gives you nothing back, and that's fine. the insertions are the gift. ๐
๐ฅ Complexity ๐ฅ
Nยทlog(a.size() + N), where N is ranges::distance(rg). math ate, fr fr. ๐งฎโจ
๐ง Notes ๐ง
the feature-test macro table has these columns: Feature-test macro, Value, Std, Feature. and the entry is:
__cpp_lib_containers_rangesโ value202202L; std(C++23); featureRanges-aware construction and insertion. obsessed with a ranges-aware moment. ๐
๐ฆ Example ๐ฆ
// Run this code
#include <iostream>
#include <map>
#include <utility>
void println(auto, auto const& container)
{
for (const auto& [key, value] : container)
std::cout << '{' << key << ',' << value << '}' << ' ';
std::cout << '\n';
}
int main()
{
auto container = std::map{std::pair{1, 11}, {3, 33}, {2, 22}, {4,
44}};
const auto rg = {std::pair{-1, -11}, {3, -33}, {-2, -22}};
#ifdef __cpp_lib_containers_ranges
container.insert_range(rg);
#else
container.insert(rg.begin(), rg.end());
#endif
println("{}", container);
}
Output:
{-2,-22} {-1,-11} {1,11} {2,22} {3,33} {4,44}
๐ See also ๐
the page's SEE ALSO section lists inserts elements, then insert or nodes with (since C++17) and (public member function) tags. that's the exact tea as printed โ iykyk. ๐ต
๐ sourced from http://cppreference.com, dated 2024.06.10 โ mwah, bye bestie! ๐
๐
๐ฆ 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