Tag: map()
C++ : Map Tutorial Part 1: Usage Detail with examples
In this article we see how & why to use std::map in c++. std::map Introduction std::map is an associative container that store elements in key-value pair. Advertisements Benefits of using std::map : It stores only unique keys and that too in sorted order based on its assigned sorting criteria. As keys are in sorted order …
C++ : Map Tutorial Part 1: Usage Detail with examplesRead More
C++ : Map Tutorial -Part 2: Map and External Sorting Criteria / Comparator
In this article we will discuss how to use external sorting criteria for keys in std::map and points that we need to take care with external sorting criteria. Default sorting criteria for keys in std::map is operator “<” i.e. std::less<T>. So, while creating std::map if we don’t specify the external sorting criteria then default criteria …
C++ : Map Tutorial -Part 2: Map and External Sorting Criteria / ComparatorRead More
C++ : Map Tutorial Part 3: Using User defined class objects as keys in std::map
In this article we will discuss how to use User defined classes as key in std::map. By default std::map uses “operator <” as sorting criteria for keys. For default data types like int and std::string etc, operator < is available by default but for User defined classes operator < is not available by default. Advertisements …
C++ : Map Tutorial Part 3: Using User defined class objects as keys in std::mapRead More
Set vs Map : How to choose a right associative container ?
In this article we will discuss how set and map are different and what factors we need to keep in mind while choosing right associative container. Let’s start from set, Set : Advertisements Set is an associative container which we need to store unique elements. It always keeps the elements in sorted order. Internally it …
Set vs Map : How to choose a right associative container ?Read More
How to Iterate over a map in C++
In this article we will discuss 3 different ways to Iterate over a map in C++. Suppose we have a map of string and int as key-value pair i.e. Advertisements std::map<std::string, int> mapOfWordCount; Now let’s see how to iterate over this map in 3 different ways i.e. Iterate over a map using STL Iterator First …
C++ Map Insert Example
In this article we will discuss how to insert a key value pair in std::map. Map internally store elements in a pair of key value i.e. Advertisements std::pair<key Type, Value Type> So, to add an element in map we can use one of its member function insert() i.e. pair<iterator,bool> insert (const value_type& element); It accepts …
How to iterate a map in reverse order – C++
In this article we will discuss how to iterate a map in reverse order. Map store the elements in sorted order of keys. For example, if you store following elements in map i.e. Advertisements {“aaa”, 10}, {“ddd”, 11}, {“bbb”, 12}, {“ccc”, 13} It will internally store the elements in sorted order of keys i.e. {“aaa”, …
How to Sort a Map by Value in C++
In this article we will discuss how to sort a map by value in both ascending and descending order. A Map store the elements in the sorted order of keys. Advertisements For example, we have a map of words and its frequency count as key – value pair i.e. std::map<std::string, int> mapOfWordCount ; Now lets insert following …
C++ : How to Initialize a map in one line using initialzer_list ?
In this article we will discuss how to initialize a map with std::initialzer_list<T>. Let’s create a map of string as key and int as value and initialize it with initializer_list i.e. Advertisements // Initialize a Map of string & int using initializer_list std::map<std::string, int> mapOfMarks = { {“Riti”,2}, {“Jack”,4} }; Here compiler will create following …
C++ : How to Initialize a map in one line using initialzer_list ?Read More
How check if a given key exists in a Map | C++
In this article we will discuss how to find if a given key exists in map or not. Map internally store elements in Key-Value pair. It provides 2 member functions to check if a given key exists in map i.e. Advertisements std::map::find std::map::count Check if map contains a key using std::map::count std::map provides a member …