Modern C++ is very powerful with many great features to help you write programs more easily with safer template class objects that have faster algorithms and methods. In C++, Containers are the powerful data storage arrays in modern C++ and they are very useful for iterating and searching data with their amazing methods, algorithms, and properties. A container is a holder object that stores data elements (a collection of data objects). std::vector
, std::array
, std::map
are these kinds of containers. There is another useful container, in this post, we explain what std::multimap
is and how you can use it with multiple keys. First, let’s refresh our memories on what containers are in C++.
Table of Contents
What is a container in modern C++?
Containers are modern data storage arrays in modern C++ and they are very useful to iterate and search data with their amazing methods and properties.
A container is a holder object that stores data elements (a collection of data objects). They are implemented as a class template to define objects that can be used with modern rules of C++ (The rule of 6), they allow great flexibility in the different data types supported as elements, and they can be used with int, float, double, etc. or with struct types, they can be used with other modern types of C++, lambdas and templates. Thus, the developer can create different data sets in memory, these can be static or dynamic, and they are safe and optimized well.
What are the basic container types modern C++?
The C++ Standards library defines 4 container types.
- Sequence Containers ( vector, array, deque, list, forward_list )
- Associative Containers ( map, multimap, set, multiset )
- Unordered Associative Containers ( unordered_map, unordered_multimap, unordered_set, unordered_multiset )
- Container Adapters ( stack, queue, priority_queue )
If you want to learn more about these each type, here are some basic details,
What is multimap (std::multimap) in modern C++?
The multimap
container (std::multimap)
is an associative container class template defined in <map>
header that contains a sorted list of key-value pairs where keys can be same values. Note that std::multimap
can use multiple same key values while std::map
uses single unique key values.
Syntax for the std::multimap in general,
1 2 3 4 5 6 7 |
template< class Key, class T, class Compare = std::less<Key>, class Allocator = std::allocator<std::pair< const Key, T> > > class multimap; |
Syntax for the std::multimap since C++17,
1 2 3 4 5 6 7 8 9 10 11 |
namespace pmr { template< class Key, class T, class Compare = std::less<Key> > using multimap = std::multimap< Key, T, Compare, std::pmr::polymorphic_allocator< std::pair<const Key, T > > >; } |
The std::multimap function is a very strong sorted associative container. As the same in std::map keys are sorted, the compare function can be used to compare key values. We can use begin()
, end()
, … iterations, and ranges to operate and list them. We can use the insert
or insert_range()
method to add pair values. There are useful modifier methods, such as; clear()
, emplace()
, erase()
, swap()
, extract()
, merge()
operations, they may have logarithmic complexity. There is a find method to search by a key value and other lookup methods. Let’s see how we can use it with some simple examples.
Is there a simple multimap (std::multimap) example in modern C++?
Here is a simple example how to declare a multimap with a int and string pair.
1 2 3 |
std::multimap< int, std::string > mp; |
Here is how you can add a pair on runtime.
1 2 3 |
mp.insert( std::pair<int, std::string>( 1, "Dogs")); |
or simply we can use { and } to set pair values as below.
1 2 3 |
mp.insert( { 1, "Dogs" } ); |
Is there a simple multimap (std::multimap) example in modern C++?
Assume that we have zones (1, 2, and 3) on our farm and we want to list a group of animals (“Cats”, “Dogs”, “Chickens”, “Cows”, … ) in these zones. Here is a full example of how we can use std::multimap in this example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <map> #include <string> int main() { std::multimap< int, std::string > mp; // mp.insert( std::pair<int, std::string>( 1, "Dogs")); mp.insert( { 1, "Dogs" } ); mp.insert( { 1, "Cats" } ); mp.insert( { 2, "Cats" } ); mp.insert( { 2, "Chickens" } ); mp.insert( { 3, "Dogs" } ); mp.insert( { 3, "Cats" } ); mp.insert( { 3, "Cows" } ); for (const auto& [key, val] : mp) { std::cout << key << " , " << val << std::endl; } system("pause"); return 0; } |
and the output will be as follows:
1 2 3 4 5 6 7 8 9 10 |
1 , Dogs 1 , Cats 2 , Cats 2 , Chickens 3 , Dogs 3 , Cats 3 , Cows Press any key to continue . . . |
Here, 1, 2, 3 values are the first
part of this pair and they are sorted key values that can be searched in any faster algorithm. Animals are the second
part of this pair.
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.