- 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 - Initializer-list Constructor
Description
The multiset::multiset() initializer list constructor in C++ STL creates a new multiset. It initializes the multiset with elements from a list of values at the time of multiset construction. It is used to create and initialize a multiset in a single statement.
Syntax
Following is the syntax of multiset initializer list constructor −
multiset(initializer_list<value_type> init); // C++11 multiset(initializer_list<value_type> init, const Compare& comp); // C++11 multiset(initializer_list<value_type> init, const Allocator& alloc); // C++11
Return value
The initializer list constructor does not return any value.
Exceptions
It may throw exceptions if memory allocation fails or if comparison operations throw exceptions.
Time complexity
The time complexity of multiset initializer list constructor is O(n log n), where n is the number of elements in the initializer list.
Examples of multiset Initializer-list Constructor
The following examples demonstrate the usage of multiset initializer list constructor:
Creating an Integer Multiset
In this example, we have initialized a list of values at the beginning to create a new multiset of integers.
#include <iostream>
#include <set>
using namespace std;
void printMs(const multiset<int> &numbers) {
for (auto it = numbers.begin(); it != numbers.end(); ++it)
cout << " " << *it;
cout << endl;
}
int main() {
multiset<int> numbers = {50, 20, 30, 20, 10, 50, 40};
cout << "Multiset size: " << numbers.size() << endl;
cout << "Multiset elements:";
printMs(numbers);
return 0;
}
The output of the above code is given below −
Multiset size: 7 Multiset elements: 10 20 20 30 40 50 50
Creating a String Multiset
Here is an example of using the initializer list constructor to create a multiset of strings with duplicate elements −
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<string> fruits = {"apple", "banana", "apple", "cherry", "banana", "apple"};
cout << "Multiset size: " << fruits.size() << endl;
cout << "Multiset elements:";
for (const auto &fruit : fruits)
cout << " " << fruit;
cout << endl;
return 0;
}
The output of the above code is given below −
Multiset size: 6 Multiset elements: apple apple apple banana banana cherry
Using Initializer-list Constructor with Custom Comparator
The following example demonstrates creating a multiset using an initializer list with a custom comparison function to sort elements in descending order −
#include <iostream>
#include <set>
#include <functional>
using namespace std;
int main() {
multiset<int, greater<int>> nums = {15, 25, 10, 25, 30, 15};
cout << "Multiset size: " << nums.size() << endl;
cout << "Multiset: ";
for (int x : nums)
cout << x << " ";
cout << endl;
}
The output of the above code is given below −
Multiset size: 6 Multiset: 30 25 25 15 15 10
Creating a Multiset of Custom Objects
Here is an example of initializer list constructor to create a multiset of custom Point objects with duplicate values −
#include <iostream>
#include <set>
using namespace std;
// A simple structure to store a point
struct Point {
int x, y;
};
struct ComparePoint {
bool operator()(const Point &a, const Point &b) const {
if (a.x < b.x)
return true;
if (a.x > b.x)
return false;
return a.y < b.y; // Compare y if x is same
}
};
int main(){
multiset<Point, ComparePoint> points = {{1, 2}, {1, 5}, {0, 0}, {1, 2}};
cout << "Multiset size: " << points.size() << endl;
cout << "Multiset elements:\n";
for (const Point &p : points){
cout << "(" << p.x << ", " << p.y << ") ";
}
cout << endl;
}
The output of the above code is given below −
Multiset size: 4 Multiset elements: (0, 0) (1, 2) (1, 2) (1, 5)