[Development] QHash iteration vs std::unordered_map

NIkolai Marchenko enmarantispam at gmail.com
Sun Apr 16 17:08:54 CEST 2017


Very unlikely I'd say. Hashes are used all over the place in ppl' code and
making this change will break way too much to be sensible.

On Sun, Apr 16, 2017 at 6:05 PM, Mark Gaiser <markg85 at gmail.com> wrote:

> On Sun, Apr 16, 2017 at 4:44 PM, Corentin <corentin.jabot at gmail.com>
> wrote:
> > Funny, a friend at kdab asked me about that exact question a few minutes
> > ago.
> > The reason for the difference is most certainly an historical one ( and
> > can't be changed because it would break quite a bit of code ).
> >
> > If you want the same behavior, you can create a proxy for your
> associative
> > container instance, with a custom iterator whose operator*() returns a
> std
> > pair ( or a QPair ) - quite a bit of boilterplate code.
>
> It probably does come down to historic reasons, but that would be
> strange as well.
> I doubt that the C++ committee changed what is returned by operator*()
> (correct me if i'm wrong) so i'm inclined to think that the
> std::<container> stuff is returning it as an std::pair for quite a
> while now.
> That again makes me wonder, why did Qt diverge from that?
>
> And... if Qt plans to change it in Qt6?
>
> >
> >
> >
> >
> > Le dim. 16 avr. 2017 à 15:57, Mark Gaiser <markg85 at gmail.com> a écrit :
> >>
> >> 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
> >> _______________________________________________
> >> Development mailing list
> >> Development at qt-project.org
> >> http://lists.qt-project.org/mailman/listinfo/development
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20170416/c8665756/attachment.html>


More information about the Development mailing list