0

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  
Ba360
  • 44
  • 7
  • 1
    It's easy to overlook things while in the throes of debugging, but note that the output from the map has the keys in increasing numerical order. That's not a coincidence. – Pete Becker May 08 '21 at 13:04

2 Answers2

4

std::map does manage the ordering of the elements according to the keys <, or a custom comparator. Thats actually the main feature of a std::map. If you don't want that then don't use a std::map. If you want to keep the elements in order of insertion you could use a std::vector< std::pair< double, unsigned int>> instead. Just beware of the different complexity guarantees you get from the different containers for eg insertion, lookup, iteration, removal, etc.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

std::map stores elements in order of the key. It so happens that you generated numbers in a nearly ascending order.

Caleth
  • 52,200
  • 2
  • 44
  • 75