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. How to insert different arrays into a QVector.
Forum Updated to NodeBB v4.3 + New Features

How to insert different arrays into a QVector.

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.8k Views 1 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.
  • S Offline
    S Offline
    Shankar B
    wrote on 17 Oct 2019, 09:39 last edited by
    #1

    I have two arrays
    example:
    a[3]={1,2,3};
    b[5]={4,5,6,7,8};

    i want to insert into QVector . Later i need to retrieve each element of respective arrays from the Qvector. I am getting the size of the vector as 2 but I am unable to retrieve each element of array. Any help will be most appreciated.

    J P 2 Replies Last reply 17 Oct 2019, 09:41
    1
    • S Shankar B
      17 Oct 2019, 09:39

      I have two arrays
      example:
      a[3]={1,2,3};
      b[5]={4,5,6,7,8};

      i want to insert into QVector . Later i need to retrieve each element of respective arrays from the Qvector. I am getting the size of the vector as 2 but I am unable to retrieve each element of array. Any help will be most appreciated.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 17 Oct 2019, 09:41 last edited by
      #2

      Hi @Shankar-B
      and welcome,

      can you share some more code, with what you already did, and what you actually want :)


      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
      1
      • S Shankar B
        17 Oct 2019, 09:39

        I have two arrays
        example:
        a[3]={1,2,3};
        b[5]={4,5,6,7,8};

        i want to insert into QVector . Later i need to retrieve each element of respective arrays from the Qvector. I am getting the size of the vector as 2 but I am unable to retrieve each element of array. Any help will be most appreciated.

        P Offline
        P Offline
        Pl45m4
        wrote on 17 Oct 2019, 09:53 last edited by Pl45m4
        #3

        @Shankar-B

        How do you insert the values into your QVector? With push_back(val)?

        If you need array "a" and "b" in one container as such, try a 2D-Vector or Vector of Vectors.

        You can access them by using

        QVector< QVector<int> > vec;
        // fill vector
        // [...]
        // Access elements
        for(int i = 0; i < vec.size(); i++)
        {
          for(int j = 0; j = vec.at(i).size(); j++)
          {
               qDebug() << vec.at(i).at(j);
          }
        }
        
        

        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Shankar B
          wrote on 17 Oct 2019, 09:54 last edited by
          #4

          int a[3]={1,2,3};
          int b[5]={4,5,6,7,8};

              QVector<int> vec;
              vec.push_back(a[3]);
               vec.push_back(b[5]);
          
               int s = vec.size();
                   for (int i=0;i<s;i++)
                   {
                       qDebug() << vec[i][i];
          
                   }
          

          Since am new to this i am not getting how to access each element of the corresponding array when they merged in to vector. Above lines of code i was trying these thing.

          J 1 Reply Last reply 17 Oct 2019, 09:56
          0
          • S Shankar B
            17 Oct 2019, 09:54

            int a[3]={1,2,3};
            int b[5]={4,5,6,7,8};

                QVector<int> vec;
                vec.push_back(a[3]);
                 vec.push_back(b[5]);
            
                 int s = vec.size();
                     for (int i=0;i<s;i++)
                     {
                         qDebug() << vec[i][i];
            
                     }
            

            Since am new to this i am not getting how to access each element of the corresponding array when they merged in to vector. Above lines of code i was trying these thing.

            J Offline
            J Offline
            JonB
            wrote on 17 Oct 2019, 09:56 last edited by JonB
            #5

            @Shankar-B

            vec[i][i];
            

            Use of i for both indexes is wrong! And will indeed only print out first 2 elements of the arrays in the 2-element vector.

            You want more like (untested)

                     for (int i=0;i<s;i++)
                         for (int j = 0; j < vec[i].size(); j++)
                         {
                             qDebug() << vec[i][j];
                         }
            

            Further:

                vec.push_back(a[3]);
                 vec.push_back(b[5]);
            

            This is only pushing one element from each a/b array into the vector. I think you're wanting to push whole arrays, like vec.push_back(a)? In which case declaration QVector<int> vec will be wrong....?

            1 Reply Last reply
            1
            • S Offline
              S Offline
              Shankar B
              wrote on 17 Oct 2019, 10:04 last edited by
              #6

              int a[3]={1,2,3};
              int b[5]={4,5,6,7,8};

                 QVector< QVector<int> > vec;
                  vec.push_back(a);
                   vec.push_back(b);
              
              
              
                       for (int i=0;i< vec.size();i++)
                       {
                           for(int j = 0; j = vec.at(i).size(); j++)
                            {
                                 qDebug() << vec.at(i).at(j);
                            }
              
                       }
              
                       }
              

              when ever am compiling these thing am getting below error.

              D:\Qt Projects\Testingexample\mainwindow.cpp:41: error: no matching function for call to 'QVector<QVector<int> >::push_back(int [3])'
              vec.push_back(a);

              J P 2 Replies Last reply 17 Oct 2019, 10:09
              0
              • S Shankar B
                17 Oct 2019, 10:04

                int a[3]={1,2,3};
                int b[5]={4,5,6,7,8};

                   QVector< QVector<int> > vec;
                    vec.push_back(a);
                     vec.push_back(b);
                
                
                
                         for (int i=0;i< vec.size();i++)
                         {
                             for(int j = 0; j = vec.at(i).size(); j++)
                              {
                                   qDebug() << vec.at(i).at(j);
                              }
                
                         }
                
                         }
                

                when ever am compiling these thing am getting below error.

                D:\Qt Projects\Testingexample\mainwindow.cpp:41: error: no matching function for call to 'QVector<QVector<int> >::push_back(int [3])'
                vec.push_back(a);

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 17 Oct 2019, 10:09 last edited by J.Hilk
                #7

                @Shankar-B
                well, yes, you're mixing and matching at will.

                int a[3]={1,2,3}; != QVector<int>
                

                I would suggest not to use pure c arrays, use the vector container for it. If you need to pass the raw array around later on, you can get that by calling data() of the container

                QVector<int> a ={1,2,3};
                    QVector<int> b ={4,5,6,7,8};
                
                    QVector<QVector<int> > vec;
                    vec.push_back(a);
                    vec.push_back(b);
                
                    int s = vec.size();
                    for (int i=0;i<s;i++)
                    {
                        auto element = vec.at(i);
                        for(int j(0); j < element.size(); j++)
                            qDebug() << i << element[j];
                        qDebug () << endl;
                
                    }
                

                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
                • S Shankar B
                  17 Oct 2019, 10:04

                  int a[3]={1,2,3};
                  int b[5]={4,5,6,7,8};

                     QVector< QVector<int> > vec;
                      vec.push_back(a);
                       vec.push_back(b);
                  
                  
                  
                           for (int i=0;i< vec.size();i++)
                           {
                               for(int j = 0; j = vec.at(i).size(); j++)
                                {
                                     qDebug() << vec.at(i).at(j);
                                }
                  
                           }
                  
                           }
                  

                  when ever am compiling these thing am getting below error.

                  D:\Qt Projects\Testingexample\mainwindow.cpp:41: error: no matching function for call to 'QVector<QVector<int> >::push_back(int [3])'
                  vec.push_back(a);

                  P Offline
                  P Offline
                  Pl45m4
                  wrote on 17 Oct 2019, 10:10 last edited by Pl45m4
                  #8

                  @Shankar-B

                  Because you have a matrix-like vector now, you need to change push_back(x) to something like:

                  QVector< QVector<int> > vec;
                  
                  // static indices not a clean solution but just to demontrate
                  vec.at(0).push_back(i);
                  vec.at(1).push_back(j);
                  

                  But you need to interate through your input arrays first.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Shankar B
                    wrote on 17 Oct 2019, 10:14 last edited by
                    #9

                    I tried the above response from J.Hilk and it is working fine. Thanks to all from their prompt replies which helped me in resolving this issue.

                    J 1 Reply Last reply 17 Oct 2019, 10:31
                    2
                    • S Shankar B
                      17 Oct 2019, 10:14

                      I tried the above response from J.Hilk and it is working fine. Thanks to all from their prompt replies which helped me in resolving this issue.

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 17 Oct 2019, 10:31 last edited by
                      #10

                      @Shankar-B great don't forget to use the topic tools to set the topic to solved :)


                      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
                      • S Offline
                        S Offline
                        Shankar B
                        wrote on 21 Oct 2019, 11:02 last edited by
                        #11

                        How to convert const QVector<quint32> to vector<int>.
                        example:
                        std::vector<int> l_sensorAddressList= static_cast<quint32>p_sensorAddressList.toStdVector();

                        J 1 Reply Last reply 21 Oct 2019, 11:32
                        0
                        • S Shankar B
                          21 Oct 2019, 11:02

                          How to convert const QVector<quint32> to vector<int>.
                          example:
                          std::vector<int> l_sensorAddressList= static_cast<quint32>p_sensorAddressList.toStdVector();

                          J Offline
                          J Offline
                          JonB
                          wrote on 21 Oct 2019, 11:32 last edited by
                          #12

                          @Shankar-B
                          You raised a dedicated topic https://forum.qt.io/topic/107950/how-to-convert-const-qvector-quint32-to-vector-int for this question. That's where it belongs, not as a post in the topic too.

                          1 Reply Last reply
                          0

                          8/12

                          17 Oct 2019, 10:10

                          • Login

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