Prerequisite: Map in STL
A map is a container which is used to store a key-value pair. By default, In Primitive datatypes such as int, char, bool, float in C/C++ are undefined if variables are not initialized, But in a Map, every key is mapped with default value zero when the map is declared. To initialize the map with a random default value below is the approach:
Approach:
- Declare a structure(say struct node) with a default value.
- Intialise Map with key mapped to struct node.
Syntax:
// For Structure
struct Node {
int value = -1;
}
// For Map with every key mapped to default value -1
Map < int, Node > M;
Below is the illustration of the Map with a default value -1:
// C++ program to illustrate a Map // initialise with default value #include <bits/stdc++.h> using namespace std; // Structure Node struct Node { int value = -1; }; // Driver Code int main() { // Map initialise with key value // pair with each pair mapped with // structure Node map<int, Node> Map; // Print the default value of 1 // store in Map cout << Map[1].value << endl; return 0; } |
-1
Recommended Posts:
- All possible values of floor(N/K) for all values of K
- Count of all possible values of X such that A % X = B
- Number of values of b such that a = b + (a^b)
- How to traverse through all values for a given key in multimap?
- Find the values of X and Y in the Given Equations
- Sum of even values and update queries on an array
- Sum of all natural numbers from L to R ( for large values of L and R )
- Does STL priority queue allow duplicate values?
- Sum of decomposition values of all suffixes of an Array
- Print values of 'a' in equation (a+b) <= n and a+b is divisible by x
- Queries for elements having values within the range A to B using MO's Algorithm
- Longest subarray with difference exactly K between any two distinct values
- How to delete a range of values from the List using Iterator
- Frequency of each element of an array of small ranged values
- Construct a Matrix of size NxN with values in range [1, N^2] as per given conditions
- Find duplicates in an Array with values 1 to N using counting sort
- Sum of N-terms of geometric progression for larger values of N | Set 2 (Using recursion)
- Find Partition Line such that sum of values on left and right is equal
- Largest subsequence such that all indices and all values are multiples individually
- Minimum number of coins that can generate all the values in the given range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


