Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. sorting a QList of QMaps
Forum Updated to NodeBB v4.3 + New Features

sorting a QList of QMaps

Scheduled Pinned Locked Moved Solved Mobile and Embedded
4 Posts 3 Posters 699 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marc_Van_Daele
    wrote on last edited by
    #1

    The following code snippet compiles fine (and works as expected) when compiling on Linux using g++

        QList<QMap<QString, QVariant>> steps;
        std::sort(steps.cbegin(), steps.cend(), [](QMap<QString, QVariant> step1, QMap<QString, QVariant> step2) -> bool
        {
            int ts1 = step1["ts"].toInt();
            int ts2 = step2["ts"].toInt();
            return ts1<ts2;
        });
    
    

    However, when I compile this for Android using clang, this fails with a number of errors starting with

    <snip>/Qt/Android/android-ndk-r19c/sources/cxx-stl/llvm-libc++/include/algorithm:3832:17: error: no matching function for call to 'swap'
                    swap(*__first, *__last);
                    ^~~~
    <snip>/Qt/Android/android-ndk-r19c/sources/cxx-stl/llvm-libc++/include/algorithm:4017:5: note: in instantiation of function template specialization 'std::__ndk1::__sort<(lambda at ../Apps/TradfriLuxMain.cpp:43:45) &, QList<QMap<QString, QVariant> >::const_iterator>' requested here
        __sort<_Comp_ref>(__first, __last, __comp);
        ^
    Main.cpp:43:10: note: in instantiation of function template specialization 'std::__ndk1::sort<QList<QMap<QString, QVariant> >::const_iterator, (lambda at Main.cpp:43:45)>' requested here
        std::sort(steps.cbegin(), steps.cend(), [](QMap<QString, QVariant> step1, QMap<QString, QVariant> step2) -> bool
             ^
    ...
    

    Any ideas what this rather cryptic error means and how to fix it?

    Thanks in advance,

    Marc

    JKSHJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I would pass step1 and step2 as const ref instead copying the data all the time.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • M Marc_Van_Daele

        The following code snippet compiles fine (and works as expected) when compiling on Linux using g++

            QList<QMap<QString, QVariant>> steps;
            std::sort(steps.cbegin(), steps.cend(), [](QMap<QString, QVariant> step1, QMap<QString, QVariant> step2) -> bool
            {
                int ts1 = step1["ts"].toInt();
                int ts2 = step2["ts"].toInt();
                return ts1<ts2;
            });
        
        

        However, when I compile this for Android using clang, this fails with a number of errors starting with

        <snip>/Qt/Android/android-ndk-r19c/sources/cxx-stl/llvm-libc++/include/algorithm:3832:17: error: no matching function for call to 'swap'
                        swap(*__first, *__last);
                        ^~~~
        <snip>/Qt/Android/android-ndk-r19c/sources/cxx-stl/llvm-libc++/include/algorithm:4017:5: note: in instantiation of function template specialization 'std::__ndk1::__sort<(lambda at ../Apps/TradfriLuxMain.cpp:43:45) &, QList<QMap<QString, QVariant> >::const_iterator>' requested here
            __sort<_Comp_ref>(__first, __last, __comp);
            ^
        Main.cpp:43:10: note: in instantiation of function template specialization 'std::__ndk1::sort<QList<QMap<QString, QVariant> >::const_iterator, (lambda at Main.cpp:43:45)>' requested here
            std::sort(steps.cbegin(), steps.cend(), [](QMap<QString, QVariant> step1, QMap<QString, QVariant> step2) -> bool
                 ^
        ...
        

        Any ideas what this rather cryptic error means and how to fix it?

        Thanks in advance,

        Marc

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by JKSH
        #3

        @Marc_Van_Daele said in sorting a QList of QMaps:

        error: no matching function for call to 'swap'
                        swap(*__first, *__last);
                        ^~~~
        

        Behind the scenes, std::sort() calls std::swap() to move your elements around. However, std::swap() cannot be used with const iterators.

        std::sort(steps.cbegin(), steps.cend())
        

        To fix it, convert your const iterators to non-const iterators: std::sort(steps.begin(), steps.end())

        [](QMap<QString, QVariant> step1, QMap<QString, QVariant> step2) -> bool {...}
        

        This is valid code, but I agree with @Christian-Ehrlicher: Pass the parameters by const-reference, not by-value. This avoids making copies of your maps every time your lambda is called.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        4
        • M Offline
          M Offline
          Marc_Van_Daele
          wrote on last edited by
          #4

          Thanks!
          Using begin()/end() indeed compiles fine!
          Thanks also for pointing out the (unintended) copy. I've switched to const-reference

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved