std::distance goes wrong for unordered map

48 Views Asked by At

What's wrong with std::distacne in this code snippet?

As far as I know, for std::map or std::vector, it's ok to invoke std::distance. But for this code snippet, the program will break down if std::distance is called.

// constructing unordered_multisets
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
std::unordered_multiset<std::string> sixth ( {"red","green","blue"} ); // range


auto itr = sixth.begin();
itr++;


int dis = std::distance(itr, sixth.begin()); //What's wrong?!
std::cout << ">>"<< dis << std::endl;

std::cout << "game over" << std::endl;

return 0;
0

There are 0 best solutions below