- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <unordered_multiset>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ multiset Library - Assignment Operator(=)
Description
The multiset::assignment operator(=) in C++ STL is used to assign the elements of one multiset to another. While copying the elements, it replaces the current elements of the multiset with the elements of another multiset or initializer list. It copies or moves the elements by maintaining its sorted order.
Syntax
Following is the syntax of multiset assignment operator −
multiset& operator=(const multiset& other); multiset& operator=(multiset&& other); multiset& operator=(initializer_list<value_type> ilist);
Return value
The operator returns a reference to the current multiset object (*this).
Exceptions
The operator may throw exceptions if element copy/move operations throw.
Time complexity
The time complexity of multiset operator= is O(n), where n is the number of elements being assigned.
Examples of multiset operator=
The following examples demonstrate the usage of multiset operator= −
Assign One Multiset to Another
In this example, we have created a multiset of integers nums1 and used the assignment operator to assign its elements to another multiset nums2.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> nums1 = {50, 20, 30, 20, 10, 50, 40};
multiset<int> nums2;
cout << "Original multiset:";
for (auto it = nums1.begin(); it != nums1.end(); ++it)
cout << " " << *it;
cout << endl;
// Using copy assignment operator
nums2 = nums1;
cout << "Copied multiset:";
for (auto it = nums2.begin(); it != nums2.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
The output of the above code is given below −
Original multiset: 10 20 20 30 40 50 50 Copied multiset: 10 20 20 30 40 50 50
Assigning Empty Multiset to a Non-empty Multiset
In this example, we have a non-empty multiset nums1. Using the assignment operator, we assign an empty multiset empty to existing non-empty multiset that clears the non-empty multiset making its size 0.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> nums1 = {1, 2, 3, 4};
cout << "Original multiset elements: ";
for (int x : nums1)
cout << x << " ";
cout << endl;
cout << "Original multiset Size: " << nums1.size();
// Creating an empty multiset
multiset<int> empty;
// Assigning empty multiset to nums1
nums1 = empty;
cout << "\nSize after assignment: " << nums1.size();
}
Original multiset elements: 1 2 3 4 Original multiset Size: 4 Size after assignment: 0
C++ Multiset Assignment Using Initializer List
In this example, we have used the assignment operator with an initializer list to add new values to an existing multiset numbers.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> numbers = {5, 15, 25};
cout << "Initial multiset: ";
for (int x : numbers)
cout << x << " ";
cout << endl;
// Assignment using initializer list
numbers = {10, 20, 30, 20, 10};
cout << "After assignment: ";
for (int x : numbers)
cout << x << " ";
cout << endl;
return 0;
}
The output of the above code is given below −
Initial multiset: 5 15 25 After assignment: 10 10 20 20 30