I am trying to understand why the map changes the first element position to the fourth. When I print the map, the first added position goes to the fourth one.
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <iostream>
typedef std::map<double, unsigned int> A;
double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
Here is the main function:
int main ()
{
int i, number;
double c;
number = 5;
A a;
for( i = 0 ; i < number ; i++ )
{
c = fRand(1, 9);
a[c];
std::cout << c<<"\n";
}
for(std::map<double, unsigned int>::const_iterator it = a.begin();
it != a.end(); ++it)
{
std::cout << it->first << " -> "<< it->second <<"\n";
}
return(0);
}
Answer : The first Element went to the fourth position
**7.7215** first
4.15506
7.26479
7.38752
8.29318
4.15506 -> 0
7.26479 -> 0
7.38752 -> 0
**7.7215 -> 0** fourth
8.29318 -> 0