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
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
add a comment |
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
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 vectorpairs
is created with size zero, and nothing in your code changes that. Evaluatingpairs[x]
therefore gives undefined behaviour for any value ofx
, as does doing any operation on it (i.e.pairs[x].push_back(tuple)
). You need to resizepairs
so it has enough elements before trying to manipluate the vectors it contains.
– Peter
Sep 8 '18 at 0:24
add a comment |
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
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
c++ vector std-pair push-back
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 vectorpairs
is created with size zero, and nothing in your code changes that. Evaluatingpairs[x]
therefore gives undefined behaviour for any value ofx
, as does doing any operation on it (i.e.pairs[x].push_back(tuple)
). You need to resizepairs
so it has enough elements before trying to manipluate the vectors it contains.
– Peter
Sep 8 '18 at 0:24
add a comment |
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 vectorpairs
is created with size zero, and nothing in your code changes that. Evaluatingpairs[x]
therefore gives undefined behaviour for any value ofx
, as does doing any operation on it (i.e.pairs[x].push_back(tuple)
). You need to resizepairs
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
Reading the std::vector reference for operator []...
"Unlike std::map::operator[], this operator never inserts a new element into the container."
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Sep 8 '18 at 0:17
Anon MailAnon Mail
4,29911218
4,29911218
add a comment |
add a comment |
Reading the std::vector reference for operator []...
"Unlike std::map::operator[], this operator never inserts a new element into the container."
add a comment |
Reading the std::vector reference for operator []...
"Unlike std::map::operator[], this operator never inserts a new element into the container."
add a comment |
Reading the std::vector reference for operator []...
"Unlike std::map::operator[], this operator never inserts a new element into the container."
Reading the std::vector reference for operator []...
"Unlike std::map::operator[], this operator never inserts a new element into the container."
answered Mar 6 at 23:16
Madison A.Madison A.
86
86
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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. Evaluatingpairs[x]
therefore gives undefined behaviour for any value ofx
, as does doing any operation on it (i.e.pairs[x].push_back(tuple)
). You need to resizepairs
so it has enough elements before trying to manipluate the vectors it contains.– Peter
Sep 8 '18 at 0:24