Namespaces
Variants

std::map<Key,T,Compare,Allocator>:: insert_range

From cppreference.net

template < container-compatible-range < value_type > R >
void insert_range ( R && rg ) ;
(C++23以降)
(constexprはC++26以降)

rg の範囲内の各要素のコピーを挿入するのは、 * this 内にその要素のキーと等価なキーを持つ要素が存在しない場合に限ります。

範囲内の各イテレータは rg に対して正確に1回デリファレンスされます。

以下のいずれかの条件が満たされる場合、動作は未定義です:

イテレータおよび参照は無効化されません。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳 - HTMLタグ、属性、
タグ内のテキストは翻訳せず保持
- C++固有の用語(Parameters, Complexity, Notes, Example, See also)は翻訳せず保持
- 元のフォーマットと構造を完全に維持

パラメータ

rg - a container compatible range , that is, an input_range whose elements are convertible to T

計算量

N·log(S+N) 、ここで S size ( ) N ranges:: distance ( rg ) です。

注記

機能テスト マクロ 標準 機能
__cpp_lib_containers_ranges 202202L (C++23) Ranges-aware 構築と挿入

#include <iostream>
#include <map>
#include <utility>
void println(auto, const auto& 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);
}

出力:

{-2,-22} {-1,-11} {1,11} {2,22} {3,33} {4,44}

関連項目

要素を挿入 またはノード (C++17以降)
(公開メンバ関数)