Doubt - Map operation in C++

Asked

Viewed 87 times

0

In order to compare different pairs of shoes I created a map, the first int represents size and the char represents whether it is for the left or right foot ('e' or’d').

N represents the number of different shoes.

In the test below I noticed that if I enter two shoes of the same size the first is maintained and the second is not. For what reason this occurs?

#include <iostream>
#include <map>

int main(){

    int n, m,i=0;
    char l;
    std::map<int,char> calcado;

    std::cin>>n;

    for(i=0;i<n;i++){
        std::cin>>m;
        std::cin>>l;
        calcado.insert ( std::pair<int,char>(m,l) );
    }
    auto it = calcado.begin();
    auto en = calcado.begin();

    std::cout<<"Comecando!!!\n";

    for(auto it = calcado.cbegin(); it != calcado.cend(); ++it){
        std::cout << "NEW!\n";
        std::cout << it->first << " " << it->second<< "\n";
        std::cout << "Entrando no loop\n";
        for(auto en = calcado.cbegin(); en != calcado.cend(); ++en){
            std::cout << en->first << " " << en->second<< "\n";
        }
    }
}

Input:

3
40
d
40
e
45
d

Output:

Comecando!!!
NEW!
40 d
Entrando no loop
40 d
45 d
NEW!
45 d
Entrando no loop
40 d
45 d
  • Opa Leandro, the first item of a map is the key of it, it is not possible to put 2 equal, because there will be no way to search later. I believe that for your algorithm is not the best data structure.

  • Thanks André!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.