Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

iterating in map/unordered map c++

// CPP program to traverse a unordered_map using
// range based for loop
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    unordered_map<int, int> m;
    for (int i = 0; i < n; i++)
        m[arr[i]]++;
 
    cout << "Element Frequency" << endl;
    for (auto i : m)
        cout << i.first << "    " << i.second
             << endl;
             
   //OR using begin() and end()
   
   for (auto i = m.begin(); i != m.end(); i++)
        cout << i->first << "      " << i->second
             << endl;
 
    return 0;
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #iterating #map
ADD COMMENT
Topic
Name
7+4 =