Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Casting QVector<int> to QVector<double>
Forum Updated to NodeBB v4.3 + New Features

Casting QVector<int> to QVector<double>

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 6 Posters 3.5k Views 3 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.
  • D Offline
    D Offline
    dovahin
    wrote on last edited by
    #1

    Is there an elegant way of casting QVector<int> to QVector<double> ? I know I could iterate through both of them, but looking for something better

    kshegunovK 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Why do you need to do that for ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      0
      • D dovahin

        Is there an elegant way of casting QVector<int> to QVector<double> ? I know I could iterate through both of them, but looking for something better

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @dovahin said in Casting QVector<int> to QVector<double>:

        I know I could iterate through both of them, but looking for something better

        What @SGaist wrote. Also there's nothing better. QVector<int> and QVector<double> are two different and distinct classes, you can't just cast from one to the other.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        4
        • SGaistS SGaist

          Hi and welcome to devnet,

          Why do you need to do that for ?

          D Offline
          D Offline
          dovahin
          wrote on last edited by
          #4

          @SGaist because I have QVector<int> and I need to pass it to qwt_plot_currve function setSamples which takes QVector<double>

          jsulmJ J.HilkJ 2 Replies Last reply
          0
          • D dovahin

            @SGaist because I have QVector<int> and I need to pass it to qwt_plot_currve function setSamples which takes QVector<double>

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @dovahin Either you use QVector<double> from the beginning, then there is no need to cast anything. Or you will have to iterate over the element of your QVector<int> and add each element to the QVector<double> which isn't efficient.
            As @kshegunov said both classes are different and you can't simply cast one to the other.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • D dovahin

              @SGaist because I have QVector<int> and I need to pass it to qwt_plot_currve function setSamples which takes QVector<double>

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @dovahin

              you could try to initialize your QVectpr<double>(myOtherVector.size()) and than try direct data manipulation via T *QVector::data() but you can not simply memcopy the data over. It won't be the same size and is not reinterpret_cast compatible.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

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

                The propose idea to use QVector<double> in the first place is the fastest one. But if you really need to convert such a vector:

                QVector<int> in;
                QVector<double> out;
                out.reserve(in.size());
                std::copy(in.cbegin(), in.cend(), std::back_inserter(out));
                

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

                D 1 Reply Last reply
                6
                • Christian EhrlicherC Christian Ehrlicher

                  The propose idea to use QVector<double> in the first place is the fastest one. But if you really need to convert such a vector:

                  QVector<int> in;
                  QVector<double> out;
                  out.reserve(in.size());
                  std::copy(in.cbegin(), in.cend(), std::back_inserter(out));
                  
                  D Offline
                  D Offline
                  dovahin
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher thanks, thats solve my problem :)

                  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