[Development] QHash iteration vs std::unordered_map
Mark Gaiser
markg85 at gmail.com
Sun Apr 16 15:56:16 CEST 2017
Hi,
Take this simple example:
QHash<int, QString> test = {
{10, "aaa"},
{20, "bbb"},
{30, "ccc"}
};
for (const auto &entry: qAsConst(test)) {
qDebug() << entry;
}
It returns:
"aaa"
"ccc"
"bbb"
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:
std::pair(30,"ccc")
std::pair(10,"aaa")
std::pair(20,"bbb")
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.
Thanks,
Mark
More information about the Development
mailing list