[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/container/map/operator%3D [Back]  [Original]

std::map::operator= cppreference.com
cppreference.com

std::map::operator=

cppreference.com
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>
map& operator=( const map& other );
(1)
(2)
map& operator=( map&& other );
( C++11)
( C++17)
map& operator=( map&& other ) noexcept(/* */);
( C++17)
map& operator=( std::initializer_list<value_type> ilist );
(3) ( C++11)

1) . other. std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value true, . , *this , other . ( C++11).
2) . other, (.. other ). other , . std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value true, . false , , , , . *this .
3) ilist.

other ,
ilist ,

*this

1) *this other.
2) *this, , , *this other.
3) O(NlogN) , N size() + ilist.size(). ilist - value_comp().


2)
noexcept:  
noexcept(std::allocator_traits<Allocator>::is_always_equal::value && std::is_nothrow_move_assignable<Compare>::value)
( C++17)

( (2)), , , , ( end ) other , , *this. [container.requirements.general]/12, [lwg-2321].


operator= , std::map :

#include <map>
#include <iostream>
 
void display_sizes(char const* comment,
                   const std::map<int, int>& nums1,
                   const std::map<int, int>& nums2,
                   const std::map<int, int>& nums3)
{
    std::cout << comment
              << " nums1: " << nums1.size() << ','
              << " nums2: " << nums2.size() << ','
              << " nums3: " << nums3.size() << '\n';
}
 
void display(char const* comment, const std::map<int, int>& v)
{
    std::cout << comment << "{ ";
    for (const auto &p : v) {
        std::cout << "{" << p.first << ", " << p.second << "} ";
    }
    std::cout << "}\n";
}
 
int main()
{
    std::map<int, int> nums1 {{3, 1}, {4, 1}, {5, 9}, 
                              {6, 1}, {7, 1}, {8, 9}};
    std::map<int, int> nums2;
    std::map<int, int> nums3;
 
    display_sizes(":\n", nums1, nums2, nums3);
 
    //     nums1  nums2
    nums2 = nums1;
 
    display_sizes(" :\n", nums1, nums2, nums3);
 
    //      nums1  nums3,
    // ,  nums1,   nums3
    nums3 = std::move(nums1);
 
    display_sizes("  :\n", nums1, nums2, nums3);
 
    display(" nums3 = ", nums3);
 
    //        nums3
    nums3 = {{1, 3}, {2, 2}, {3, 1}};
 
    display("  initializer_list \n nums3 = ", nums3);
}

:

:
 nums1: 6, nums2: 0, nums3: 0
 :
 nums1: 6, nums2: 6, nums3: 0
  :
 nums1: 0, nums2: 6, nums3: 6
 nums3 = { {3, 1} {4, 1} {5, 9} {6, 1} {7, 1} {8, 9} }
  initializer_list 
 nums3 = { {1, 3} {2, 2} {3, 1} }

.

map
(public -) []

Web Proxy Viewer  |  New URL  |  Original Page