名前空間
変種
操作

std::multiset<Key,Compare,Allocator>::merge

From cppreference.com
 
 
 
 
template< class C2 >
void merge( std::set<Key, C2, Allocator>& source );
(1) (C++17以降)
template< class C2 >
void merge( std::set<Key, C2, Allocator>&& source );
(2) (C++17以降)
template< class C2 >
void merge( std::multiset<Key, C2, Allocator>& source );
(3) (C++17以降)
template< class C2 >
void merge( std::multiset<Key, C2, Allocator>&& source );
(4) (C++17以降)

source の各要素を抽出し("splice")、*this の比較オブジェクトを使用して *this に挿入しようとします。

要素はコピーも移動もされず、コンテナノードの内部ポインタのみが再設定されます。転送された要素へのすべてのポインタと参照は有効なままで、source ではなく *this を参照するようになります。

get_allocator() != source.get_allocator() の場合、動作は未定義である。

目次

[編集] パラメータ

source - ノードを転送する互換性のあるコンテナ

[編集] 戻り値

(なし)

例外

比較が例外をスローしない限り、例外をスローしない。

[編集] 計算量

N * log(size() + N))。ただし、N は source.size() である。

[編集]

#include <iostream>
#include <set>
 
// print out a container
template<class Os, class K>
Os& operator<<(Os& os, const std::multiset<K>& v)
{
    os << '[' << v.size() << "] {";
    bool o{};
    for (const auto& e : v)
        os << (o ? ", " : (o = 1, " ")) << e;
    return os << " }\n";
}
 
int main()
{
    std::multiset<char>
        p{'C', 'B', 'B', 'A'}, 
        q{'E', 'D', 'E', 'C'};
 
    std::cout << "p: " << p << "q: " << q;
 
    p.merge(q);
 
    std::cout << "p.merge(q);\n" << "p: " << p << "q: " << q;
}

出力

p: [4] { A, B, B, C }
q: [4] { C, D, E, E }
p.merge(q);
p: [8] { A, B, B, C, C, D, E, E }
q: [0] { }

[編集] 関連項目

(C++17)
コンテナからノードを抽出する
(公開メンバ関数) [編集]
要素 またはノード(C++17以降)を挿入する
(公開メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)