jeudi 1 juin 2017

Why unordered_map in C++ gives unexpected output?

I got the wrong output for the following program. The program should produce output 11 20 27 32 35 36 35 32 27 20 11 but it produces 11 11 20 27 32 35 36 35 32 27 20. When I use just map it produces correct output.I have no clue why this happened.
Compiling environment:
Ubuntu 14.04 32 bit
g++ -std=c++11 filename.cpp -o filename

#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
int n = 11;
unordered_map<int, int>m;
for (int i = 1; i <= n; i++)
{
    for (int j = i; j <= n; j++)
    {
        for (int k = i; k <= j; k++)
        {
            m[k]++;
        }
    }
}
for (auto it = m.begin(); it != m.end(); it++)
{
    cout << it->second << " ";
}
return 0;
}

Aucun commentaire:

Enregistrer un commentaire