How to set up vector of vector pairs?2019 Community Moderator ElectionHow do you set, clear, and toggle a single bit?What is the equivalent of the C++ Pair<L,R> in Java?How do I iterate over the words of a string?How to find out if an item is present in a std::vector?Appending a vector to a vectorC++ Vectors: Why is this piece of code not working?C++ how to push_back an array int[10] to std::vector<int[10]>?Initialising std::pair<double, std::array<std::pair<double, double>, 3> >Vector push_back error when compilingVector Stack Pair | Longest path in a tree using dfs

Determine voltage drop over 10G resistors with cheap multimeter

Why is participating in the European Parliamentary elections used as a threat?

Unfrosted light bulb

What (if any) is the reason to buy in small local stores?

What is it called when someone votes for an option that's not their first choice?

Error in master's thesis, I do not know what to do

How do you justify more code being written by following clean code practices?

Hackerrank All Women's Codesprint 2019: Name the Product

Symbolism of 18 Journeyers

What is the reasoning behind standardization (dividing by standard deviation)?

Hot air balloons as primitive bombers

Should a narrator ever describe things based on a characters view instead of fact?

Why doesn't the fusion process of the sun speed up?

Is xar preinstalled on macOS?

Have the tides ever turned twice on any open problem?

label a part of commutative diagram

What is the difference between something being completely legal and being completely decriminalized?

Does fire aspect on a sword, destroy mob drops?

Writing in a Christian voice

What are the rules for concealing thieves' tools (or items in general)?

What will the Frenchman say?

Knife as defense against stray dogs

When did hardware antialiasing start being available?

Print a physical multiplication table



How to set up vector of vector pairs?



2019 Community Moderator ElectionHow do you set, clear, and toggle a single bit?What is the equivalent of the C++ Pair<L,R> in Java?How do I iterate over the words of a string?How to find out if an item is present in a std::vector?Appending a vector to a vectorC++ Vectors: Why is this piece of code not working?C++ how to push_back an array int[10] to std::vector<int[10]>?Initialising std::pair<double, std::array<std::pair<double, double>, 3> >Vector push_back error when compilingVector Stack Pair | Longest path in a tree using dfs










0















In this code, I am trying to make a vector of vector pairs. The code compiles but it has a segmentation fault and I cannot figure out where I am going wrong. I would be grateful for any hint that can solve my problem.



#include <iostream>
#include <vector>
using namespace std;
vector<vector<pair<int,bool> > > pairs;

void insert(int x, int y)

pair<int,bool> tuple=make_pair(y,0);
pairs[x].push_back(tuple);


void pairing()

for(int i=0; i<12; i++)

for(int j=0; j<12; j++)

insert(i,j);




int main()

pairing();
return 0;










share|improve this question
























  • std::vector<std::vector<std::pair<int,bool>>> pairs(12, 0, false, 1, false, .., 10, false, 11, false); (you probably want to create function to create inner vector).

    – Jarod42
    Sep 8 '18 at 0:24












  • The vector pairs is created with size zero, and nothing in your code changes that. Evaluating pairs[x] therefore gives undefined behaviour for any value of x, as does doing any operation on it (i.e. pairs[x].push_back(tuple)). You need to resize pairs so it has enough elements before trying to manipluate the vectors it contains.

    – Peter
    Sep 8 '18 at 0:24















0















In this code, I am trying to make a vector of vector pairs. The code compiles but it has a segmentation fault and I cannot figure out where I am going wrong. I would be grateful for any hint that can solve my problem.



#include <iostream>
#include <vector>
using namespace std;
vector<vector<pair<int,bool> > > pairs;

void insert(int x, int y)

pair<int,bool> tuple=make_pair(y,0);
pairs[x].push_back(tuple);


void pairing()

for(int i=0; i<12; i++)

for(int j=0; j<12; j++)

insert(i,j);




int main()

pairing();
return 0;










share|improve this question
























  • std::vector<std::vector<std::pair<int,bool>>> pairs(12, 0, false, 1, false, .., 10, false, 11, false); (you probably want to create function to create inner vector).

    – Jarod42
    Sep 8 '18 at 0:24












  • The vector pairs is created with size zero, and nothing in your code changes that. Evaluating pairs[x] therefore gives undefined behaviour for any value of x, as does doing any operation on it (i.e. pairs[x].push_back(tuple)). You need to resize pairs so it has enough elements before trying to manipluate the vectors it contains.

    – Peter
    Sep 8 '18 at 0:24













0












0








0








In this code, I am trying to make a vector of vector pairs. The code compiles but it has a segmentation fault and I cannot figure out where I am going wrong. I would be grateful for any hint that can solve my problem.



#include <iostream>
#include <vector>
using namespace std;
vector<vector<pair<int,bool> > > pairs;

void insert(int x, int y)

pair<int,bool> tuple=make_pair(y,0);
pairs[x].push_back(tuple);


void pairing()

for(int i=0; i<12; i++)

for(int j=0; j<12; j++)

insert(i,j);




int main()

pairing();
return 0;










share|improve this question
















In this code, I am trying to make a vector of vector pairs. The code compiles but it has a segmentation fault and I cannot figure out where I am going wrong. I would be grateful for any hint that can solve my problem.



#include <iostream>
#include <vector>
using namespace std;
vector<vector<pair<int,bool> > > pairs;

void insert(int x, int y)

pair<int,bool> tuple=make_pair(y,0);
pairs[x].push_back(tuple);


void pairing()

for(int i=0; i<12; i++)

for(int j=0; j<12; j++)

insert(i,j);




int main()

pairing();
return 0;







c++ vector std-pair push-back






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 22:44









Brian Tompsett - 汤莱恩

4,2421339102




4,2421339102










asked Sep 8 '18 at 0:10









user143user143

143




143












  • std::vector<std::vector<std::pair<int,bool>>> pairs(12, 0, false, 1, false, .., 10, false, 11, false); (you probably want to create function to create inner vector).

    – Jarod42
    Sep 8 '18 at 0:24












  • The vector pairs is created with size zero, and nothing in your code changes that. Evaluating pairs[x] therefore gives undefined behaviour for any value of x, as does doing any operation on it (i.e. pairs[x].push_back(tuple)). You need to resize pairs so it has enough elements before trying to manipluate the vectors it contains.

    – Peter
    Sep 8 '18 at 0:24

















  • std::vector<std::vector<std::pair<int,bool>>> pairs(12, 0, false, 1, false, .., 10, false, 11, false); (you probably want to create function to create inner vector).

    – Jarod42
    Sep 8 '18 at 0:24












  • The vector pairs is created with size zero, and nothing in your code changes that. Evaluating pairs[x] therefore gives undefined behaviour for any value of x, as does doing any operation on it (i.e. pairs[x].push_back(tuple)). You need to resize pairs so it has enough elements before trying to manipluate the vectors it contains.

    – Peter
    Sep 8 '18 at 0:24
















std::vector<std::vector<std::pair<int,bool>>> pairs(12, 0, false, 1, false, .., 10, false, 11, false); (you probably want to create function to create inner vector).

– Jarod42
Sep 8 '18 at 0:24






std::vector<std::vector<std::pair<int,bool>>> pairs(12, 0, false, 1, false, .., 10, false, 11, false); (you probably want to create function to create inner vector).

– Jarod42
Sep 8 '18 at 0:24














The vector pairs is created with size zero, and nothing in your code changes that. Evaluating pairs[x] therefore gives undefined behaviour for any value of x, as does doing any operation on it (i.e. pairs[x].push_back(tuple)). You need to resize pairs so it has enough elements before trying to manipluate the vectors it contains.

– Peter
Sep 8 '18 at 0:24





The vector pairs is created with size zero, and nothing in your code changes that. Evaluating pairs[x] therefore gives undefined behaviour for any value of x, as does doing any operation on it (i.e. pairs[x].push_back(tuple)). You need to resize pairs so it has enough elements before trying to manipluate the vectors it contains.

– Peter
Sep 8 '18 at 0:24












2 Answers
2






active

oldest

votes


















0














pairs has no elements so you can't do this: pairs[x].



Either resize the pairs vector so it has N blank vector<pair<int,bool> > in it, or create a vector<pair<int,bool> > first and push it back into pairs






share|improve this answer






























    0














    Reading the std::vector reference for operator []...



    "Unlike std::map::operator[], this operator never inserts a new element into the container."






    share|improve this answer






















      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%2f52230917%2fhow-to-set-up-vector-of-vector-pairs%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      pairs has no elements so you can't do this: pairs[x].



      Either resize the pairs vector so it has N blank vector<pair<int,bool> > in it, or create a vector<pair<int,bool> > first and push it back into pairs






      share|improve this answer



























        0














        pairs has no elements so you can't do this: pairs[x].



        Either resize the pairs vector so it has N blank vector<pair<int,bool> > in it, or create a vector<pair<int,bool> > first and push it back into pairs






        share|improve this answer

























          0












          0








          0







          pairs has no elements so you can't do this: pairs[x].



          Either resize the pairs vector so it has N blank vector<pair<int,bool> > in it, or create a vector<pair<int,bool> > first and push it back into pairs






          share|improve this answer













          pairs has no elements so you can't do this: pairs[x].



          Either resize the pairs vector so it has N blank vector<pair<int,bool> > in it, or create a vector<pair<int,bool> > first and push it back into pairs







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 8 '18 at 0:17









          Anon MailAnon Mail

          4,29911218




          4,29911218























              0














              Reading the std::vector reference for operator []...



              "Unlike std::map::operator[], this operator never inserts a new element into the container."






              share|improve this answer



























                0














                Reading the std::vector reference for operator []...



                "Unlike std::map::operator[], this operator never inserts a new element into the container."






                share|improve this answer

























                  0












                  0








                  0







                  Reading the std::vector reference for operator []...



                  "Unlike std::map::operator[], this operator never inserts a new element into the container."






                  share|improve this answer













                  Reading the std::vector reference for operator []...



                  "Unlike std::map::operator[], this operator never inserts a new element into the container."







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 6 at 23:16









                  Madison A.Madison A.

                  86




                  86



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52230917%2fhow-to-set-up-vector-of-vector-pairs%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

                      Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                      Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                      Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved