Namespaces
Variants

std::unordered_set<Key,Hash,KeyEqual,Allocator>:: operator=

From cppreference.net

unordered_set & operator = ( const unordered_set & other ) ;
(1) (C++11부터)
(C++26부터 constexpr)
(2)
unordered_set & operator = ( unordered_set && other ) ;
(C++11부터)
(C++17까지)
unordered_set & operator = ( unordered_set && other )
noexcept ( /* 아래 참조 */ ) ;
(C++17부터)
(C++26부터 constexpr)
unordered_set & operator = ( std:: initializer_list < value_type > ilist ) ;
(3) (C++11부터)
(C++26부터 constexpr)

컨테이너의 내용을 대체합니다.

traits std:: allocator_traits < allocator_type > 로 정의합니다:

1) 복사 할당 연산자. 내용을 other 의 내용 복사본으로 대체합니다.
만약 traits :: propagate_on_container_copy_assignment :: value true 라면, * this 의 할당자는 other 의 복사본으로 대체됩니다. 만약 할당 후 * this 의 할당자가 이전 값과 비교하여 같지 않다면, 이전 할당자를 사용하여 메모리를 해제한 후 새로운 할당자를 사용하여 요소를 복사하기 전에 메모리를 할당합니다. 그렇지 않으면, * this 가 소유한 메모리는 가능한 경우 재사용될 수 있습니다. 어떤 경우든 원래 * this 에 속했던 요소들은 파괴되거나 요소별 복사 할당으로 대체될 수 있습니다.
2) 이동 할당 연산자. 이동 의미론을 사용하여 내용을 other 의 내용으로 대체합니다(즉, other 의 데이터가 other 에서 이 컨테이너로 이동됩니다). other 는 이후 유효하지만 지정되지 않은 상태가 됩니다.
만약 traits :: propagate_on_container_move_assignment :: value true 이면, * this 의 할당자는 other 의 할당자 사본으로 대체됩니다. 만약 false 이고 * this other 의 할당자가 동일하지 않다면, * this other 가 소유한 메모리의 소유권을 취할 수 없으며, 필요에 따라 자체 할당자를 사용하여 추가 메모리를 할당하면서 각 요소를 개별적으로 이동 할당해야 합니다. 어떤 경우든 원래 * this 에 속했던 모든 요소는 파괴되거나 요소별 이동 할당으로 대체됩니다.
3) 내용을 초기화자 목록 ilist 로 식별된 내용으로 대체합니다.

목차

매개변수

other - 데이터 소스로 사용할 다른 컨테이너
ilist - 데이터 소스로 사용할 초기화 리스트

반환값

* this

복잡도

1) * this other 의 크기에 선형적으로 비례합니다.
2) * this 의 크기에 선형적으로 비례하며, 단 할당자(allocator)가 서로 같지 않고 전파(propagate)되지 않는 경우에는 * this other 의 크기에 선형적으로 비례합니다.
3) * this ilist 의 크기에 선형적입니다.

예외

2)
noexcept 명세:
noexcept ( std:: allocator_traits < Allocator > :: is_always_equal :: value

&& std:: is_nothrow_move_assignable < Hash > :: value

&& std:: is_nothrow_move_assignable < Pred > :: value )
(C++17부터)

참고 사항

컨테이너 이동 대입 후 (오버로드 ( 2 ) ), 호환되지 않는 할당자에 의한 요소별 이동 대입이 강제되지 않는 한, other 에 대한 참조, 포인터 및 반복자(끝 반복자 제외)는 유효하게 남아 있지만, 이제 * this 에 있는 요소를 참조합니다. 현재 표준은 [container.reqmts]/67 의 포괄적 명시문을 통해 이 보장을 제공하며, LWG 이슈 2321 를 통해 더 직접적인 보장이 검토 중입니다.

예제

다음 코드는 operator = 를 사용하여 하나의 std::unordered_set 을 다른 것에 할당합니다:

#include <initializer_list>
#include <iostream>
#include <iterator>
#include <unordered_set>
void print(const auto comment, const auto& container)
{
    auto size = std::size(container);
    std::cout << comment << "{ ";
    for (const auto& element : container)
        std::cout << element << (--size ? ", " : " ");
    std::cout << "}\n";
}
int main()
{
    std::unordered_set<int> x{1, 2, 3}, y, z;
    const auto w = {4, 5, 6, 7};
    std::cout << "Initially:\n";
    print("x = ", x);
    print("y = ", y);
    print("z = ", z);
    std::cout << "Copy assignment copies data from x to y:\n";
    y = x;
    print("x = ", x);
    print("y = ", y);
    std::cout << "Move assignment moves data from x to z, modifying both x and z:\n";
    z = std::move(x);
    print("x = ", x);
    print("z = ", z);
    std::cout << "Assignment of initializer_list w to z:\n";
    z = w;
    print("w = ", w);
    print("z = ", z);
}

가능한 출력:

Initially:
x = { 3, 2, 1 }
y = { }
z = { }
Copy assignment copies data from x to y:
x = { 3, 2, 1 }
y = { 3, 2, 1 }
Move assignment moves data from x to z, modifying both x and z:
x = { }
z = { 3, 2, 1 }
Assignment of initializer_list w to z:
w = { 4, 5, 6, 7 }
z = { 7, 6, 5, 4 }

참고 항목

unordered_set 을 생성합니다
(public member function)