multiset_multiset
multiset_multiset (std::multiset::multiset — 생성자)
std::multiset 객체를 만드는 생성자들이에요. 기본 생성자부터 비교자, 이터레이터 범위, 초기화 목록, 할당자, 복사·이동까지 다양한 방식으로 초기화할 수 있어요.
출처: cppreference
본문
주요 시그니처는 다음과 같아요.
multiset();
explicit multiset( const Compare& comp, const Allocator& alloc = Allocator() ); // (2)
explicit multiset( const Allocator& alloc ); // (3) (since C++11)
template< class InputIt >
multiset( InputIt first, InputIt last,
const Compare& comp = Compare(),
const Allocator& alloc = Allocator() ); // (4)
multiset( std::initializer_list<value_type> init,
const Compare& comp = Compare(),
const Allocator& alloc = Allocator() ); // (5) (since C++11)
multiset( const multiset& other ); // (6)
multiset( multiset&& other ); // (7) (since C++11)
template< container-compatible-range<value_type> R >
multiset( std::from_range_t, R&& rg,
const Compare& comp = Compare(),
const Allocator& alloc = Allocator() ); // (from_range)
여러 오버로드가 있는데, 핵심은 다음과 같아요.
- 기본 생성자: 빈 컨테이너를 만들어요. C++11부터
multiset(Compare())으로 위임해요. - 비교자/할당자만 받는 생성자: 지정한 비교자와 할당자로 빈 컨테이너를 만들어요.
- (4) 범위
[first, last)의 원소들을 가진 컨테이너를 만들어요. - (5) 초기화 목록
init의 원소들을 가진 컨테이너를 만들어요. - (6)(7) 복사/이동 생성자.
- from_range 생성자: 범위
rg의 원소들을 가진 컨테이너를 만들어요.
멀티셋은 중복 키를 허용하므로, 정렬할 때 같은 키를 병합하지 않아요. multiset의 복잡도는 여러 생성자에 걸쳐 트리 구성이 필요하면 N·log(N) (N은 원소 개수)예요.
예제
#include <iostream>
#include <set>
int main()
{
std::multiset<int> s{1, 1, 2}; // 초기화 목록, 중복 허용
for (int x : s) std::cout << x << ' ';
}