[Development] QHash iteration vs std::unordered_map

Mark Gaiser markg85 at gmail.com
Sun Apr 16 15:56:16 CEST 2017


Take this simple example:

  QHash<int, QString> test = {
    {10, "aaa"},
    {20, "bbb"},
    {30, "ccc"}

  for (const auto &entry: qAsConst(test)) {
    qDebug() << entry;

It returns:

and the std::unordered_map version:
  std::unordered_map<int, QString> test = {
    {10, "aaa"},
    {20, "bbb"},
    {30, "ccc"}

  for (const auto &entry: test) {
    qDebug() << entry;

it returns:

As you can see, the QHash iteration directly returns the value. The
std::unordered_map returns a std::par with the key and value for the
current iteration (in it's respective first and second members). Both
approaches probably have arguments in favor and against. What i'm
curious about is why there is a difference at all?

I'm curious because the behavior is a bit unexpected when compared to
std::unordered_map. I would have guessed QHash to follow the same
logic as std::unordered_map, only with a Qt syntax. So for instance a
return of QPair(...), not the value directly.


More information about the Development mailing list