Appending a value to a nested QList












0














I am trying to append values to a QList inside another QList but it doesn't seem to work?



Here is a MCVE of my problem where I try to append int values:



#include <QList>
#include <QDebug>

int main(int argc, char *argv) {

QList<QList<int>> my_list;

int result;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 4; ++j) {
result = i * j;
my_list.value(i).push_back(result);
qDebug() << my_list.size() << "," << my_list.value(i).size() << " : " << my_list.value(i).value(j);
}
}

return 0;
}


This yields:



Starting C:Users ... buildreleasename_of_the_app.exe...
0 , 0 : 0
0 , 0 : 0
0 , 0 : 0
0 , 0 : 0
0 , 0 : 0
0 , 0 : 0
0 , 0 : 0
0 , 0 : 0
C:Users ... buildreleasename_of_the_app.exe exited with code 0


Can anyone please tell me what I'm doing wrong?










share|improve this question





























    0














    I am trying to append values to a QList inside another QList but it doesn't seem to work?



    Here is a MCVE of my problem where I try to append int values:



    #include <QList>
    #include <QDebug>

    int main(int argc, char *argv) {

    QList<QList<int>> my_list;

    int result;
    for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 4; ++j) {
    result = i * j;
    my_list.value(i).push_back(result);
    qDebug() << my_list.size() << "," << my_list.value(i).size() << " : " << my_list.value(i).value(j);
    }
    }

    return 0;
    }


    This yields:



    Starting C:Users ... buildreleasename_of_the_app.exe...
    0 , 0 : 0
    0 , 0 : 0
    0 , 0 : 0
    0 , 0 : 0
    0 , 0 : 0
    0 , 0 : 0
    0 , 0 : 0
    0 , 0 : 0
    C:Users ... buildreleasename_of_the_app.exe exited with code 0


    Can anyone please tell me what I'm doing wrong?










    share|improve this question



























      0












      0








      0







      I am trying to append values to a QList inside another QList but it doesn't seem to work?



      Here is a MCVE of my problem where I try to append int values:



      #include <QList>
      #include <QDebug>

      int main(int argc, char *argv) {

      QList<QList<int>> my_list;

      int result;
      for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 4; ++j) {
      result = i * j;
      my_list.value(i).push_back(result);
      qDebug() << my_list.size() << "," << my_list.value(i).size() << " : " << my_list.value(i).value(j);
      }
      }

      return 0;
      }


      This yields:



      Starting C:Users ... buildreleasename_of_the_app.exe...
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      C:Users ... buildreleasename_of_the_app.exe exited with code 0


      Can anyone please tell me what I'm doing wrong?










      share|improve this question















      I am trying to append values to a QList inside another QList but it doesn't seem to work?



      Here is a MCVE of my problem where I try to append int values:



      #include <QList>
      #include <QDebug>

      int main(int argc, char *argv) {

      QList<QList<int>> my_list;

      int result;
      for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 4; ++j) {
      result = i * j;
      my_list.value(i).push_back(result);
      qDebug() << my_list.size() << "," << my_list.value(i).size() << " : " << my_list.value(i).value(j);
      }
      }

      return 0;
      }


      This yields:



      Starting C:Users ... buildreleasename_of_the_app.exe...
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      0 , 0 : 0
      C:Users ... buildreleasename_of_the_app.exe exited with code 0


      Can anyone please tell me what I'm doing wrong?







      qt nested append push-back qlist






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 18:43









      TrebuchetMS

      1,9861618




      1,9861618










      asked Nov 20 at 17:52









      Matt

      106




      106
























          1 Answer
          1






          active

          oldest

          votes


















          1














          There are two issues with this code sample:



          First:



          The container my_list hasn't been initialised yet. The line



          my_list.value(i).push_back(result);


          doesn't actually push values into the container (as we might have hoped).



          In your code, i is always out of bounds. (Since my_list will always have size 0.) As a result, and according to the documentation,




          If the index i is out of bounds, the function returns a default-constructed value.




          Since this default-constructed value isn't assigned anywhere, it will most probably be allocated on the heap and be left sitting there until destructed.



          Further accesses into my_list such as qDebug() << my_list.value(i).size() will default-construct another QList (since again, i is out of bounds).



          Make sure you already have QList<int> ready to push values into.



          Second:



          The value() method returns a const reference to the QList which does not allow modification (same for at()). If you want to push values to a QList you should use the instead of the value() method.



          The following code does what you want:



          for (int i = 0; i < 2; ++i) {
          my_list.push_back(QList<int>()); // first appends a new QList at index `i`

          for (int j = 0; j < 4; ++j) {
          result = i * j;
          my_list[i].push_back(result); // safely retrieves QList at index `i` and adds an element
          }
          }





          share|improve this answer























          • There where two issues with my code. Your answer addressed the first (major) issue (thanks!) and allowed me to further correct my code. I added it to your answer as an edit.
            – Matt
            Nov 20 at 18:42










          • I've approved the edit. 👍 Didn't realise .value() didn't provide a reference. I usually only use and .at(). :-)
            – TrebuchetMS
            Nov 20 at 18:44













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398765%2fappending-a-value-to-a-nested-qlist%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          There are two issues with this code sample:



          First:



          The container my_list hasn't been initialised yet. The line



          my_list.value(i).push_back(result);


          doesn't actually push values into the container (as we might have hoped).



          In your code, i is always out of bounds. (Since my_list will always have size 0.) As a result, and according to the documentation,




          If the index i is out of bounds, the function returns a default-constructed value.




          Since this default-constructed value isn't assigned anywhere, it will most probably be allocated on the heap and be left sitting there until destructed.



          Further accesses into my_list such as qDebug() << my_list.value(i).size() will default-construct another QList (since again, i is out of bounds).



          Make sure you already have QList<int> ready to push values into.



          Second:



          The value() method returns a const reference to the QList which does not allow modification (same for at()). If you want to push values to a QList you should use the instead of the value() method.



          The following code does what you want:



          for (int i = 0; i < 2; ++i) {
          my_list.push_back(QList<int>()); // first appends a new QList at index `i`

          for (int j = 0; j < 4; ++j) {
          result = i * j;
          my_list[i].push_back(result); // safely retrieves QList at index `i` and adds an element
          }
          }





          share|improve this answer























          • There where two issues with my code. Your answer addressed the first (major) issue (thanks!) and allowed me to further correct my code. I added it to your answer as an edit.
            – Matt
            Nov 20 at 18:42










          • I've approved the edit. 👍 Didn't realise .value() didn't provide a reference. I usually only use and .at(). :-)
            – TrebuchetMS
            Nov 20 at 18:44


















          1














          There are two issues with this code sample:



          First:



          The container my_list hasn't been initialised yet. The line



          my_list.value(i).push_back(result);


          doesn't actually push values into the container (as we might have hoped).



          In your code, i is always out of bounds. (Since my_list will always have size 0.) As a result, and according to the documentation,




          If the index i is out of bounds, the function returns a default-constructed value.




          Since this default-constructed value isn't assigned anywhere, it will most probably be allocated on the heap and be left sitting there until destructed.



          Further accesses into my_list such as qDebug() << my_list.value(i).size() will default-construct another QList (since again, i is out of bounds).



          Make sure you already have QList<int> ready to push values into.



          Second:



          The value() method returns a const reference to the QList which does not allow modification (same for at()). If you want to push values to a QList you should use the instead of the value() method.



          The following code does what you want:



          for (int i = 0; i < 2; ++i) {
          my_list.push_back(QList<int>()); // first appends a new QList at index `i`

          for (int j = 0; j < 4; ++j) {
          result = i * j;
          my_list[i].push_back(result); // safely retrieves QList at index `i` and adds an element
          }
          }





          share|improve this answer























          • There where two issues with my code. Your answer addressed the first (major) issue (thanks!) and allowed me to further correct my code. I added it to your answer as an edit.
            – Matt
            Nov 20 at 18:42










          • I've approved the edit. 👍 Didn't realise .value() didn't provide a reference. I usually only use and .at(). :-)
            – TrebuchetMS
            Nov 20 at 18:44
















          1












          1








          1






          There are two issues with this code sample:



          First:



          The container my_list hasn't been initialised yet. The line



          my_list.value(i).push_back(result);


          doesn't actually push values into the container (as we might have hoped).



          In your code, i is always out of bounds. (Since my_list will always have size 0.) As a result, and according to the documentation,




          If the index i is out of bounds, the function returns a default-constructed value.




          Since this default-constructed value isn't assigned anywhere, it will most probably be allocated on the heap and be left sitting there until destructed.



          Further accesses into my_list such as qDebug() << my_list.value(i).size() will default-construct another QList (since again, i is out of bounds).



          Make sure you already have QList<int> ready to push values into.



          Second:



          The value() method returns a const reference to the QList which does not allow modification (same for at()). If you want to push values to a QList you should use the instead of the value() method.



          The following code does what you want:



          for (int i = 0; i < 2; ++i) {
          my_list.push_back(QList<int>()); // first appends a new QList at index `i`

          for (int j = 0; j < 4; ++j) {
          result = i * j;
          my_list[i].push_back(result); // safely retrieves QList at index `i` and adds an element
          }
          }





          share|improve this answer














          There are two issues with this code sample:



          First:



          The container my_list hasn't been initialised yet. The line



          my_list.value(i).push_back(result);


          doesn't actually push values into the container (as we might have hoped).



          In your code, i is always out of bounds. (Since my_list will always have size 0.) As a result, and according to the documentation,




          If the index i is out of bounds, the function returns a default-constructed value.




          Since this default-constructed value isn't assigned anywhere, it will most probably be allocated on the heap and be left sitting there until destructed.



          Further accesses into my_list such as qDebug() << my_list.value(i).size() will default-construct another QList (since again, i is out of bounds).



          Make sure you already have QList<int> ready to push values into.



          Second:



          The value() method returns a const reference to the QList which does not allow modification (same for at()). If you want to push values to a QList you should use the instead of the value() method.



          The following code does what you want:



          for (int i = 0; i < 2; ++i) {
          my_list.push_back(QList<int>()); // first appends a new QList at index `i`

          for (int j = 0; j < 4; ++j) {
          result = i * j;
          my_list[i].push_back(result); // safely retrieves QList at index `i` and adds an element
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 at 0:19









          Matt

          106




          106










          answered Nov 20 at 18:04









          TrebuchetMS

          1,9861618




          1,9861618












          • There where two issues with my code. Your answer addressed the first (major) issue (thanks!) and allowed me to further correct my code. I added it to your answer as an edit.
            – Matt
            Nov 20 at 18:42










          • I've approved the edit. 👍 Didn't realise .value() didn't provide a reference. I usually only use and .at(). :-)
            – TrebuchetMS
            Nov 20 at 18:44




















          • There where two issues with my code. Your answer addressed the first (major) issue (thanks!) and allowed me to further correct my code. I added it to your answer as an edit.
            – Matt
            Nov 20 at 18:42










          • I've approved the edit. 👍 Didn't realise .value() didn't provide a reference. I usually only use and .at(). :-)
            – TrebuchetMS
            Nov 20 at 18:44


















          There where two issues with my code. Your answer addressed the first (major) issue (thanks!) and allowed me to further correct my code. I added it to your answer as an edit.
          – Matt
          Nov 20 at 18:42




          There where two issues with my code. Your answer addressed the first (major) issue (thanks!) and allowed me to further correct my code. I added it to your answer as an edit.
          – Matt
          Nov 20 at 18:42












          I've approved the edit. 👍 Didn't realise .value() didn't provide a reference. I usually only use and .at(). :-)
          – TrebuchetMS
          Nov 20 at 18:44






          I've approved the edit. 👍 Didn't realise .value() didn't provide a reference. I usually only use and .at(). :-)
          – TrebuchetMS
          Nov 20 at 18:44




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398765%2fappending-a-value-to-a-nested-qlist%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen