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.9k 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 Shankar B

    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.

    Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on 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 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.

      JonBJ 1 Reply Last reply
      0
      • S Shankar B

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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 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.HilkJ Pl45m4P 2 Replies Last reply
          0
          • S Shankar B

            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.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on 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

              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);

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on 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 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.HilkJ 1 Reply Last reply
                2
                • S Shankar B

                  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.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on 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 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();

                    JonBJ 1 Reply Last reply
                    0
                    • S Shankar B

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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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

                      • Login

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