Print a vector of vectors
I'm trying to print a bidimensional array in c++ but I have a problem. I followed the traditional way to print a vector vectorName.size()
inside a for loop. So the way that I follow was this.
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <time.h>
using namespace std;
void impMat(vector < vector <int> >, vector < vector <int> >);
int main ()
vector < vector <int> > A;
vector < vector <int> > B;
vector <int> temp;
for(int j = 0; j < 4; j++)
for(int i = 0; i < 5; i++)
temp.push_back(i);
A.push_back(temp);
B.push_back(temp);
impMat(A,B);
cout << endl;
return 0;
void impMat(vector < vector <int> > A,vector < vector <int> > B)
for(int i = 0; i < A.size(); i++)
for(int j = 0; j < A[i].size(); j++)
cout << A[i][j] << " ";
cout << endl;
cout << endl;
for(int i = 0; i < B.size(); i++)
for(int j = 0; j < B[i].size(); j++)
cout << B[i][j] << " ";
cout << endl;
But that's print something like this
0 1 2 3 4
0 1 2 3 4 0 1 2 3 4
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
Expected output
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
How do I could get to print correctly my vectors?
c++ arrays vector printing
|
show 5 more comments
I'm trying to print a bidimensional array in c++ but I have a problem. I followed the traditional way to print a vector vectorName.size()
inside a for loop. So the way that I follow was this.
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <time.h>
using namespace std;
void impMat(vector < vector <int> >, vector < vector <int> >);
int main ()
vector < vector <int> > A;
vector < vector <int> > B;
vector <int> temp;
for(int j = 0; j < 4; j++)
for(int i = 0; i < 5; i++)
temp.push_back(i);
A.push_back(temp);
B.push_back(temp);
impMat(A,B);
cout << endl;
return 0;
void impMat(vector < vector <int> > A,vector < vector <int> > B)
for(int i = 0; i < A.size(); i++)
for(int j = 0; j < A[i].size(); j++)
cout << A[i][j] << " ";
cout << endl;
cout << endl;
for(int i = 0; i < B.size(); i++)
for(int j = 0; j < B[i].size(); j++)
cout << B[i][j] << " ";
cout << endl;
But that's print something like this
0 1 2 3 4
0 1 2 3 4 0 1 2 3 4
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
Expected output
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
How do I could get to print correctly my vectors?
c++ arrays vector printing
2
What is the desired output? And why did you omit the actual code that prints? That seems like the part that's probably the problem
– GBlodgett
Nov 14 '18 at 2:40
3
What isA
, what isB
? Please provide a Minimal, Complete, and Verifiable example.
– Swordfish
Nov 14 '18 at 2:48
3
sizeof(B[i])/sizeof(int)
-- Why are you doing this? Avector
knows the number of elements it has by using thesize()
member function.
– PaulMcKenzie
Nov 14 '18 at 2:52
2
Are your vectors correctly filled? How are they initialised? Please create an Minimal, Complete, and Verifiable example (emphasis on 'complete' here...).
– Aconcagua
Nov 14 '18 at 2:55
2
Also sinceimpMat()
doesn't modify the vectors they should be passed asconst&
.
– Swordfish
Nov 14 '18 at 2:57
|
show 5 more comments
I'm trying to print a bidimensional array in c++ but I have a problem. I followed the traditional way to print a vector vectorName.size()
inside a for loop. So the way that I follow was this.
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <time.h>
using namespace std;
void impMat(vector < vector <int> >, vector < vector <int> >);
int main ()
vector < vector <int> > A;
vector < vector <int> > B;
vector <int> temp;
for(int j = 0; j < 4; j++)
for(int i = 0; i < 5; i++)
temp.push_back(i);
A.push_back(temp);
B.push_back(temp);
impMat(A,B);
cout << endl;
return 0;
void impMat(vector < vector <int> > A,vector < vector <int> > B)
for(int i = 0; i < A.size(); i++)
for(int j = 0; j < A[i].size(); j++)
cout << A[i][j] << " ";
cout << endl;
cout << endl;
for(int i = 0; i < B.size(); i++)
for(int j = 0; j < B[i].size(); j++)
cout << B[i][j] << " ";
cout << endl;
But that's print something like this
0 1 2 3 4
0 1 2 3 4 0 1 2 3 4
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
Expected output
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
How do I could get to print correctly my vectors?
c++ arrays vector printing
I'm trying to print a bidimensional array in c++ but I have a problem. I followed the traditional way to print a vector vectorName.size()
inside a for loop. So the way that I follow was this.
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <time.h>
using namespace std;
void impMat(vector < vector <int> >, vector < vector <int> >);
int main ()
vector < vector <int> > A;
vector < vector <int> > B;
vector <int> temp;
for(int j = 0; j < 4; j++)
for(int i = 0; i < 5; i++)
temp.push_back(i);
A.push_back(temp);
B.push_back(temp);
impMat(A,B);
cout << endl;
return 0;
void impMat(vector < vector <int> > A,vector < vector <int> > B)
for(int i = 0; i < A.size(); i++)
for(int j = 0; j < A[i].size(); j++)
cout << A[i][j] << " ";
cout << endl;
cout << endl;
for(int i = 0; i < B.size(); i++)
for(int j = 0; j < B[i].size(); j++)
cout << B[i][j] << " ";
cout << endl;
But that's print something like this
0 1 2 3 4
0 1 2 3 4 0 1 2 3 4
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
Expected output
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
How do I could get to print correctly my vectors?
c++ arrays vector printing
c++ arrays vector printing
edited Nov 14 '18 at 5:42
Shrikanth N
510211
510211
asked Nov 14 '18 at 2:39
Enrique AlvarezEnrique Alvarez
114
114
2
What is the desired output? And why did you omit the actual code that prints? That seems like the part that's probably the problem
– GBlodgett
Nov 14 '18 at 2:40
3
What isA
, what isB
? Please provide a Minimal, Complete, and Verifiable example.
– Swordfish
Nov 14 '18 at 2:48
3
sizeof(B[i])/sizeof(int)
-- Why are you doing this? Avector
knows the number of elements it has by using thesize()
member function.
– PaulMcKenzie
Nov 14 '18 at 2:52
2
Are your vectors correctly filled? How are they initialised? Please create an Minimal, Complete, and Verifiable example (emphasis on 'complete' here...).
– Aconcagua
Nov 14 '18 at 2:55
2
Also sinceimpMat()
doesn't modify the vectors they should be passed asconst&
.
– Swordfish
Nov 14 '18 at 2:57
|
show 5 more comments
2
What is the desired output? And why did you omit the actual code that prints? That seems like the part that's probably the problem
– GBlodgett
Nov 14 '18 at 2:40
3
What isA
, what isB
? Please provide a Minimal, Complete, and Verifiable example.
– Swordfish
Nov 14 '18 at 2:48
3
sizeof(B[i])/sizeof(int)
-- Why are you doing this? Avector
knows the number of elements it has by using thesize()
member function.
– PaulMcKenzie
Nov 14 '18 at 2:52
2
Are your vectors correctly filled? How are they initialised? Please create an Minimal, Complete, and Verifiable example (emphasis on 'complete' here...).
– Aconcagua
Nov 14 '18 at 2:55
2
Also sinceimpMat()
doesn't modify the vectors they should be passed asconst&
.
– Swordfish
Nov 14 '18 at 2:57
2
2
What is the desired output? And why did you omit the actual code that prints? That seems like the part that's probably the problem
– GBlodgett
Nov 14 '18 at 2:40
What is the desired output? And why did you omit the actual code that prints? That seems like the part that's probably the problem
– GBlodgett
Nov 14 '18 at 2:40
3
3
What is
A
, what is B
? Please provide a Minimal, Complete, and Verifiable example.– Swordfish
Nov 14 '18 at 2:48
What is
A
, what is B
? Please provide a Minimal, Complete, and Verifiable example.– Swordfish
Nov 14 '18 at 2:48
3
3
sizeof(B[i])/sizeof(int)
-- Why are you doing this? A vector
knows the number of elements it has by using the size()
member function.– PaulMcKenzie
Nov 14 '18 at 2:52
sizeof(B[i])/sizeof(int)
-- Why are you doing this? A vector
knows the number of elements it has by using the size()
member function.– PaulMcKenzie
Nov 14 '18 at 2:52
2
2
Are your vectors correctly filled? How are they initialised? Please create an Minimal, Complete, and Verifiable example (emphasis on 'complete' here...).
– Aconcagua
Nov 14 '18 at 2:55
Are your vectors correctly filled? How are they initialised? Please create an Minimal, Complete, and Verifiable example (emphasis on 'complete' here...).
– Aconcagua
Nov 14 '18 at 2:55
2
2
Also since
impMat()
doesn't modify the vectors they should be passed as const&
.– Swordfish
Nov 14 '18 at 2:57
Also since
impMat()
doesn't modify the vectors they should be passed as const&
.– Swordfish
Nov 14 '18 at 2:57
|
show 5 more comments
2 Answers
2
active
oldest
votes
The problem basically is how you fill your vectors:
for(int j = 0; j < 4; j++)
for(int i = 0; i < 5; i++)
temp.push_back(i);
A.push_back(temp);
B.push_back(temp);
// now temp yet contains all the values entered, so you produce:
// 0, 1, 2, 3, 4 in first loop run,
// 0, 1, 2, 3, 4 0, 1, 2, 3, 4 in second,
// ...
// most simple fix:
temp.clear();
More efficient, though, as you want to have the same data anyway:
for(int i = 0; i < 5; i++)
temp.push_back(i);
for(int i = 0; i < 4; i++)
A.push_back(temp);
B.push_back(temp);
add a comment |
Two simple ways of printg std::vector
s of std::vector
s:
#include <vector>
#include <iostream>
int main()
std::vector<std::vector<int>> foo
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4
;
// range-based for-loops:
for (auto const &row : foo)
for (auto const &col : row)
std::cout << col << ' ';
std::cout.put('n');
std::cout.put('n');
// ordinary for-loops:
for (std::size_t row; row < foo.size(); ++row)
for (std::size_t col; col < foo[row].size(); ++col)
std::cout << foo[row][col] << ' ';
std::cout.put('n');
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%2f53292417%2fprint-a-vector-of-vectors%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
The problem basically is how you fill your vectors:
for(int j = 0; j < 4; j++)
for(int i = 0; i < 5; i++)
temp.push_back(i);
A.push_back(temp);
B.push_back(temp);
// now temp yet contains all the values entered, so you produce:
// 0, 1, 2, 3, 4 in first loop run,
// 0, 1, 2, 3, 4 0, 1, 2, 3, 4 in second,
// ...
// most simple fix:
temp.clear();
More efficient, though, as you want to have the same data anyway:
for(int i = 0; i < 5; i++)
temp.push_back(i);
for(int i = 0; i < 4; i++)
A.push_back(temp);
B.push_back(temp);
add a comment |
The problem basically is how you fill your vectors:
for(int j = 0; j < 4; j++)
for(int i = 0; i < 5; i++)
temp.push_back(i);
A.push_back(temp);
B.push_back(temp);
// now temp yet contains all the values entered, so you produce:
// 0, 1, 2, 3, 4 in first loop run,
// 0, 1, 2, 3, 4 0, 1, 2, 3, 4 in second,
// ...
// most simple fix:
temp.clear();
More efficient, though, as you want to have the same data anyway:
for(int i = 0; i < 5; i++)
temp.push_back(i);
for(int i = 0; i < 4; i++)
A.push_back(temp);
B.push_back(temp);
add a comment |
The problem basically is how you fill your vectors:
for(int j = 0; j < 4; j++)
for(int i = 0; i < 5; i++)
temp.push_back(i);
A.push_back(temp);
B.push_back(temp);
// now temp yet contains all the values entered, so you produce:
// 0, 1, 2, 3, 4 in first loop run,
// 0, 1, 2, 3, 4 0, 1, 2, 3, 4 in second,
// ...
// most simple fix:
temp.clear();
More efficient, though, as you want to have the same data anyway:
for(int i = 0; i < 5; i++)
temp.push_back(i);
for(int i = 0; i < 4; i++)
A.push_back(temp);
B.push_back(temp);
The problem basically is how you fill your vectors:
for(int j = 0; j < 4; j++)
for(int i = 0; i < 5; i++)
temp.push_back(i);
A.push_back(temp);
B.push_back(temp);
// now temp yet contains all the values entered, so you produce:
// 0, 1, 2, 3, 4 in first loop run,
// 0, 1, 2, 3, 4 0, 1, 2, 3, 4 in second,
// ...
// most simple fix:
temp.clear();
More efficient, though, as you want to have the same data anyway:
for(int i = 0; i < 5; i++)
temp.push_back(i);
for(int i = 0; i < 4; i++)
A.push_back(temp);
B.push_back(temp);
answered Nov 14 '18 at 3:07
AconcaguaAconcagua
12.1k32143
12.1k32143
add a comment |
add a comment |
Two simple ways of printg std::vector
s of std::vector
s:
#include <vector>
#include <iostream>
int main()
std::vector<std::vector<int>> foo
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4
;
// range-based for-loops:
for (auto const &row : foo)
for (auto const &col : row)
std::cout << col << ' ';
std::cout.put('n');
std::cout.put('n');
// ordinary for-loops:
for (std::size_t row; row < foo.size(); ++row)
for (std::size_t col; col < foo[row].size(); ++col)
std::cout << foo[row][col] << ' ';
std::cout.put('n');
add a comment |
Two simple ways of printg std::vector
s of std::vector
s:
#include <vector>
#include <iostream>
int main()
std::vector<std::vector<int>> foo
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4
;
// range-based for-loops:
for (auto const &row : foo)
for (auto const &col : row)
std::cout << col << ' ';
std::cout.put('n');
std::cout.put('n');
// ordinary for-loops:
for (std::size_t row; row < foo.size(); ++row)
for (std::size_t col; col < foo[row].size(); ++col)
std::cout << foo[row][col] << ' ';
std::cout.put('n');
add a comment |
Two simple ways of printg std::vector
s of std::vector
s:
#include <vector>
#include <iostream>
int main()
std::vector<std::vector<int>> foo
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4
;
// range-based for-loops:
for (auto const &row : foo)
for (auto const &col : row)
std::cout << col << ' ';
std::cout.put('n');
std::cout.put('n');
// ordinary for-loops:
for (std::size_t row; row < foo.size(); ++row)
for (std::size_t col; col < foo[row].size(); ++col)
std::cout << foo[row][col] << ' ';
std::cout.put('n');
Two simple ways of printg std::vector
s of std::vector
s:
#include <vector>
#include <iostream>
int main()
std::vector<std::vector<int>> foo
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4 ,
0, 1, 2, 3, 4
;
// range-based for-loops:
for (auto const &row : foo)
for (auto const &col : row)
std::cout << col << ' ';
std::cout.put('n');
std::cout.put('n');
// ordinary for-loops:
for (std::size_t row; row < foo.size(); ++row)
for (std::size_t col; col < foo[row].size(); ++col)
std::cout << foo[row][col] << ' ';
std::cout.put('n');
answered Nov 14 '18 at 3:06
SwordfishSwordfish
9,36811436
9,36811436
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%2f53292417%2fprint-a-vector-of-vectors%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
2
What is the desired output? And why did you omit the actual code that prints? That seems like the part that's probably the problem
– GBlodgett
Nov 14 '18 at 2:40
3
What is
A
, what isB
? Please provide a Minimal, Complete, and Verifiable example.– Swordfish
Nov 14 '18 at 2:48
3
sizeof(B[i])/sizeof(int)
-- Why are you doing this? Avector
knows the number of elements it has by using thesize()
member function.– PaulMcKenzie
Nov 14 '18 at 2:52
2
Are your vectors correctly filled? How are they initialised? Please create an Minimal, Complete, and Verifiable example (emphasis on 'complete' here...).
– Aconcagua
Nov 14 '18 at 2:55
2
Also since
impMat()
doesn't modify the vectors they should be passed asconst&
.– Swordfish
Nov 14 '18 at 2:57