- 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 - Range Constructor
Description
The multiset::multiset() range constructor in C++ STL creates a multiset by copying elements from a specified range [first, last). It initializes one multiset with elements from another container or array. It automatically sorts the elements according to the comparison function. By default, std::less is used.
Syntax
Following is the syntax of multiset range constructor −
template <class InputIterator>
multiset(InputIterator first, InputIterator last,
const Compare& comp = Compare(),
const Allocator& alloc = Allocator()); // C++98
template <class InputIterator>
multiset(InputIterator first, InputIterator last,
const Allocator& alloc = Allocator()); // C++14
Return value
The range constructor does not return any value since it is a constructor.
Exceptions
It may throw exceptions if element construction, comparison, or memory allocation fails.
Time complexity
The time complexity of multiset range constructor is O(N log N), where N is the number of elements in the range.
Examples of multiset Range Constructor
The following examples demonstrate the usage of multiset range constructor −
Creating a multiset from an Array
In this example, we have created a multiset by copying elements from an array. We have used two iterators to define the range that points to the beginning and end of the array.
#include <iostream>
#include <set>
using namespace std;
void printMultiset(const multiset<int>& numbers) {
for (auto it = numbers.begin(); it != numbers.end(); ++it)
cout << " " << *it;
cout << endl;
}
int main() {
int arr[] = {50, 20, 30, 20, 10, 50, 40};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Array size: " << n << endl;
cout << "Array elements:";
for (int i = 0; i < n; i++)
cout << " " << arr[i];
cout << endl;
// Using index 1 to 4 of array
multiset<int> numbers(arr + 1, arr + 5);
cout << "\nSize of multiset: " << numbers.size() << endl;
cout << "Multiset elements:";
printMultiset(numbers);
return 0;
}
The output of the above code is given below −
Array size: 7 Array elements: 50 20 30 20 10 50 40 Size of multiset: 4 Multiset elements: 10 20 20 30
Creating a multiset from a Vector
Here is an example of using the range constructor to create a multiset from a vector −
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
vector<string> str = {"apple", "banana", "apple",
"cherry", "banana", "apple"};
cout << "Given vector elements:";
for (const auto &s : str)
cout << " " << s;
cout << endl;
multiset<string> word(str.begin(), str.end());
cout << "Multiset elements:";
for (const auto &fruit : word)
cout << " " << fruit;
return 0;
}
The output of the above code is given below −
Given vector elements: apple banana apple cherry banana apple Multiset elements: apple apple apple banana banana cherry
Converting Data Type Using Range Constructor
The following example converts the double data type of vector elements into an int multiset elements −
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
vector<double> decimals = {2.5, 3.1, 4.7, 1.2};
cout << "Given vector: ";
for (double d : decimals)
cout << d << " ";
cout << endl;
multiset<int> nums(decimals.begin(), decimals.end());
cout << "\nConverted multiset: ";
for (int x : nums)
cout << x << " ";
}
The output of the above code is given below −
Given vector: 2.5 3.1 4.7 1.2 Converted multiset: 1 2 3 4