📜  在映射无序映射c ++代码示例中迭代

📅  最后修改于: 2022-03-11 14:44:50.731000             🧑  作者: Mango

代码示例1
// CPP program to traverse a unordered_map using
// range based for loop
#include 
using namespace std;
 
int main()
{
    int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    unordered_map 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;
}