Search
 
SCRIPT & CODE EXAMPLE
 

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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: cpp unions 
Cpp :: scan line in c++ 
Cpp :: c++ constructors 
Cpp :: c++ vector average 
Cpp :: convert all characters in string to uppercase c++ 
Cpp :: max value of double c++ 
Cpp :: matplotlib hide numbers on axis 
Cpp :: sleep system function linux c++ 
Cpp :: search a word in cpp file 
Cpp :: error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ 
Cpp :: opencv c++ image write 
Cpp :: c++ random number between 0 and 1 
Cpp :: c++ segmented sieve 
Cpp :: string iterator in c++ 
Cpp :: How to find the suarray with maximum sum using divide and conquer 
Cpp :: c++ program to reverse an array 
Cpp :: delete specific row from dynamic 2d array c++ 
Cpp :: c++ binary search 
Cpp :: 2-Dimensional array in c++ 
Cpp :: vector length c++ 
Cpp :: 2d array c++ 
Cpp :: C++ Volume of a Cylinder 
Cpp :: c++ modulo positive 
Cpp :: reverse order binary tree in c++ 
Cpp :: anagram solution in c++ 
Cpp :: classes and objects in c++ 
Cpp :: how to empty string c++ 
Cpp :: classes constructor in c++ 
Cpp :: macros in c++ 
Cpp :: c++ float and double 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =