How do I append 10 arrays to form a single array in c++? The 2019 Stack Overflow Developer Survey Results Are InHow do you set, clear, and toggle a single bit?What are the differences between a pointer variable and a reference variable in C++?How do I iterate over the words of a string?The Definitive C++ Book Guide and ListWhat is the “-->” operator in C++?How do I use arrays in C++?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhy is it faster to process a sorted array than an unsorted array?
Is Cinnamon a desktop environment or a window manager? (Or both?)
Short story: child made less intelligent and less attractive
Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?
What do I do when my TA workload is more than expected?
Variable with quotation marks "$()"
Button changing its text & action. Good or terrible?
Is it okay to consider publishing in my first year of PhD?
What is the most efficient way to store a numeric range?
Geography at the pixel level
Dropping list elements from nested list after evaluation
ELI5: Why they say that Israel would have been the fourth country to land a spacecraft on the Moon and why they call it low cost?
Did any laptop computers have a built-in 5 1/4 inch floppy drive?
What do these terms in Caesar's Gallic Wars mean?
Why can I use a list index as an indexing variable in a for loop?
Why are there uneven bright areas in this photo of black hole?
Does adding complexity mean a more secure cipher?
Can an undergraduate be advised by a professor who is very far away?
If my opponent casts Ultimate Price on my Phantasmal Bear, can I save it by casting Snap or Curfew?
Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?
Is there a way to generate a uniformly distributed point on a sphere from a fixed amount of random real numbers?
How to quickly solve partial fractions equation?
Getting crown tickets for Statue of Liberty
Pokemon Turn Based battle (Python)
How to type this arrow in math mode?
How do I append 10 arrays to form a single array in c++?
The 2019 Stack Overflow Developer Survey Results Are InHow do you set, clear, and toggle a single bit?What are the differences between a pointer variable and a reference variable in C++?How do I iterate over the words of a string?The Definitive C++ Book Guide and ListWhat is the “-->” operator in C++?How do I use arrays in C++?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhy is it faster to process a sorted array than an unsorted array?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Let's say I have these 10 previously declared arrays in my code.
int arr1[] = 1,2,3,4,5,6,7,8,9,10;
int arr2[] = 1,2,3,4,5,6,7,8,9,10;
int arr3[] = 1,2,3,4,5,6,7,8,9,10;
int arr4[] = 1,2,3,4,5,6,7,8,9,10;
int arr5[] = 1,2,3,4,5,6,7,8,9,10;
int arr6[] = 1,2,3,4,5,6,7,8,9,10;
int arr7[] = 1,2,3,4,5,6,7,8,9,10;
int arr8[] = 1,2,3,4,5,6,7,8,9,10;
int arr9[] = 1,2,3,4,5,6,7,8,9,10;
int arr10[] = 1,2,3,4,5,6,7,8,9,10;
Basically, I want to append all 10 of these arrays one after another to make one single array.
ArrayOfArrays = arr1[], arr2[], arr3[], arr4[], arr5[], arr6[], arr7[], arr8[], arr9[], arr10[]
How would I go about doing this? This question might seem trivial for some, but I'm new to C++ and can not figure out how to do it. Please help and thanks in advance.
c++ c++11
add a comment |
Let's say I have these 10 previously declared arrays in my code.
int arr1[] = 1,2,3,4,5,6,7,8,9,10;
int arr2[] = 1,2,3,4,5,6,7,8,9,10;
int arr3[] = 1,2,3,4,5,6,7,8,9,10;
int arr4[] = 1,2,3,4,5,6,7,8,9,10;
int arr5[] = 1,2,3,4,5,6,7,8,9,10;
int arr6[] = 1,2,3,4,5,6,7,8,9,10;
int arr7[] = 1,2,3,4,5,6,7,8,9,10;
int arr8[] = 1,2,3,4,5,6,7,8,9,10;
int arr9[] = 1,2,3,4,5,6,7,8,9,10;
int arr10[] = 1,2,3,4,5,6,7,8,9,10;
Basically, I want to append all 10 of these arrays one after another to make one single array.
ArrayOfArrays = arr1[], arr2[], arr3[], arr4[], arr5[], arr6[], arr7[], arr8[], arr9[], arr10[]
How would I go about doing this? This question might seem trivial for some, but I'm new to C++ and can not figure out how to do it. Please help and thanks in advance.
c++ c++11
What do you mean by add all 10 of these arrays to one single array? like[A]+[B]+...+[J] = [Z]
or[Z] = [[A][B]...[J]]
– RC0993
Mar 22 at 5:00
Like this: ArrayOfArrays = arr1[], arr2[], arr3[], arr4[], arr5[], arr6[], arr7[], arr8[], arr9[], arr10[]
– bdbasinger
Mar 22 at 5:02
So, you want to create a matrix?
– Havenard
Mar 22 at 5:05
Is there a constraint of using arrays? Are the sizes of all arrays pre defined/ preknown? if not then you may use linked list. otherwise if the things are pre defined then its simple to declare a resultant array n populate it
– RC0993
Mar 22 at 5:07
Yes the sizes are predefined. How would I declare a resultant array and populate it?
– bdbasinger
Mar 22 at 5:09
add a comment |
Let's say I have these 10 previously declared arrays in my code.
int arr1[] = 1,2,3,4,5,6,7,8,9,10;
int arr2[] = 1,2,3,4,5,6,7,8,9,10;
int arr3[] = 1,2,3,4,5,6,7,8,9,10;
int arr4[] = 1,2,3,4,5,6,7,8,9,10;
int arr5[] = 1,2,3,4,5,6,7,8,9,10;
int arr6[] = 1,2,3,4,5,6,7,8,9,10;
int arr7[] = 1,2,3,4,5,6,7,8,9,10;
int arr8[] = 1,2,3,4,5,6,7,8,9,10;
int arr9[] = 1,2,3,4,5,6,7,8,9,10;
int arr10[] = 1,2,3,4,5,6,7,8,9,10;
Basically, I want to append all 10 of these arrays one after another to make one single array.
ArrayOfArrays = arr1[], arr2[], arr3[], arr4[], arr5[], arr6[], arr7[], arr8[], arr9[], arr10[]
How would I go about doing this? This question might seem trivial for some, but I'm new to C++ and can not figure out how to do it. Please help and thanks in advance.
c++ c++11
Let's say I have these 10 previously declared arrays in my code.
int arr1[] = 1,2,3,4,5,6,7,8,9,10;
int arr2[] = 1,2,3,4,5,6,7,8,9,10;
int arr3[] = 1,2,3,4,5,6,7,8,9,10;
int arr4[] = 1,2,3,4,5,6,7,8,9,10;
int arr5[] = 1,2,3,4,5,6,7,8,9,10;
int arr6[] = 1,2,3,4,5,6,7,8,9,10;
int arr7[] = 1,2,3,4,5,6,7,8,9,10;
int arr8[] = 1,2,3,4,5,6,7,8,9,10;
int arr9[] = 1,2,3,4,5,6,7,8,9,10;
int arr10[] = 1,2,3,4,5,6,7,8,9,10;
Basically, I want to append all 10 of these arrays one after another to make one single array.
ArrayOfArrays = arr1[], arr2[], arr3[], arr4[], arr5[], arr6[], arr7[], arr8[], arr9[], arr10[]
How would I go about doing this? This question might seem trivial for some, but I'm new to C++ and can not figure out how to do it. Please help and thanks in advance.
c++ c++11
c++ c++11
edited Mar 22 at 5:04
RC0993
276217
276217
asked Mar 22 at 4:58
bdbasingerbdbasinger
858
858
What do you mean by add all 10 of these arrays to one single array? like[A]+[B]+...+[J] = [Z]
or[Z] = [[A][B]...[J]]
– RC0993
Mar 22 at 5:00
Like this: ArrayOfArrays = arr1[], arr2[], arr3[], arr4[], arr5[], arr6[], arr7[], arr8[], arr9[], arr10[]
– bdbasinger
Mar 22 at 5:02
So, you want to create a matrix?
– Havenard
Mar 22 at 5:05
Is there a constraint of using arrays? Are the sizes of all arrays pre defined/ preknown? if not then you may use linked list. otherwise if the things are pre defined then its simple to declare a resultant array n populate it
– RC0993
Mar 22 at 5:07
Yes the sizes are predefined. How would I declare a resultant array and populate it?
– bdbasinger
Mar 22 at 5:09
add a comment |
What do you mean by add all 10 of these arrays to one single array? like[A]+[B]+...+[J] = [Z]
or[Z] = [[A][B]...[J]]
– RC0993
Mar 22 at 5:00
Like this: ArrayOfArrays = arr1[], arr2[], arr3[], arr4[], arr5[], arr6[], arr7[], arr8[], arr9[], arr10[]
– bdbasinger
Mar 22 at 5:02
So, you want to create a matrix?
– Havenard
Mar 22 at 5:05
Is there a constraint of using arrays? Are the sizes of all arrays pre defined/ preknown? if not then you may use linked list. otherwise if the things are pre defined then its simple to declare a resultant array n populate it
– RC0993
Mar 22 at 5:07
Yes the sizes are predefined. How would I declare a resultant array and populate it?
– bdbasinger
Mar 22 at 5:09
What do you mean by add all 10 of these arrays to one single array? like
[A]+[B]+...+[J] = [Z]
or [Z] = [[A][B]...[J]]
– RC0993
Mar 22 at 5:00
What do you mean by add all 10 of these arrays to one single array? like
[A]+[B]+...+[J] = [Z]
or [Z] = [[A][B]...[J]]
– RC0993
Mar 22 at 5:00
Like this: ArrayOfArrays = arr1[], arr2[], arr3[], arr4[], arr5[], arr6[], arr7[], arr8[], arr9[], arr10[]
– bdbasinger
Mar 22 at 5:02
Like this: ArrayOfArrays = arr1[], arr2[], arr3[], arr4[], arr5[], arr6[], arr7[], arr8[], arr9[], arr10[]
– bdbasinger
Mar 22 at 5:02
So, you want to create a matrix?
– Havenard
Mar 22 at 5:05
So, you want to create a matrix?
– Havenard
Mar 22 at 5:05
Is there a constraint of using arrays? Are the sizes of all arrays pre defined/ preknown? if not then you may use linked list. otherwise if the things are pre defined then its simple to declare a resultant array n populate it
– RC0993
Mar 22 at 5:07
Is there a constraint of using arrays? Are the sizes of all arrays pre defined/ preknown? if not then you may use linked list. otherwise if the things are pre defined then its simple to declare a resultant array n populate it
– RC0993
Mar 22 at 5:07
Yes the sizes are predefined. How would I declare a resultant array and populate it?
– bdbasinger
Mar 22 at 5:09
Yes the sizes are predefined. How would I declare a resultant array and populate it?
– bdbasinger
Mar 22 at 5:09
add a comment |
4 Answers
4
active
oldest
votes
Basically, I want to append all 10 of these arrays one after another to make one single array.
You cannot do that.
The closest you can get to that is by using std::array
.
std::array<int, 10> arr1 = 1,2,3,4,5,6,7,8,9,10;
...
std::array<int, 10> arr10 = 1,2,3,4,5,6,7,8,9,10;
std::array<std::array<int, 10>, 10> arrayOfArray = arr1, ..., arr10;
add a comment |
I want to append all 10 of these arrays one after another to make one
single array ?
You can have array of pointers like
int *ArrayOfPointers[10] = &arr1, &arr2, &arr3, &arr4, &arr5, &arr6, &arr7, &arr8, &arr9, &arr10;
Here ArrayOfPointers
is array of 10
int pointers i.e it can store address of 10
one dimension int array like arr1
, arr2
etc.
I assume there may be better method than what I'm suggesting in advance C++
for the same task.
add a comment |
Try this approach:
#include <iostream>
#include <vector>
int arr1[] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
int arr2[] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
// ...other arrays here...
// We pass a reference to a vector and return the same vector for performance reasons.
// Compiler often can optimize that into a better code.
std::vector<int> append(std::vector<int> & vec, int * data, int size)
for (int i = 0; i < size; ++i)
vec.push_back(data[i]);
return vec;
int main()
std::vector<int> data;
data = append(data, arr1, 10);
data = append(data, arr2, 10);
for (auto i : data)
std::cout << i << ", ";
std::cout << std::endl;
return 0;
Also, in C++ there are good containers for storing arrays, try searching for std::array
and std::vector
containers. First is a fixed size static array, the other one is dynamic.
add a comment |
In C++ it is unnecessary and ill-advised to use C-style arrays. For arrays of
constant size you may use std::array
and for arrays of variable size, std::vector
It looks rather as if what you actually want is a constant two-dimensional matrix
and to be able to access each of its rows as as a constant array, but do not
know how to initialise a two-dimensional matrix. If that's the case, here's how:
#include <iostream>
#include <array>
std::array<std::array<int,10>,10> matrix =
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10
;
int main()
std::array<int,10> const & arr0 = matrix[0];
for (int const & i : arr0)
std::cout << i << ' ';
std::cout << std::endl;
// Or more simply...
auto const & arr5 = matrix[5];
for (auto const & i : arr5)
std::cout << i << ' ';
std::cout << std::endl;
Compile, link and run:
$ g++ -Wall -Wextra main.cpp && ./a.out
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
live demo
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%2f55293155%2fhow-do-i-append-10-arrays-to-form-a-single-array-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Basically, I want to append all 10 of these arrays one after another to make one single array.
You cannot do that.
The closest you can get to that is by using std::array
.
std::array<int, 10> arr1 = 1,2,3,4,5,6,7,8,9,10;
...
std::array<int, 10> arr10 = 1,2,3,4,5,6,7,8,9,10;
std::array<std::array<int, 10>, 10> arrayOfArray = arr1, ..., arr10;
add a comment |
Basically, I want to append all 10 of these arrays one after another to make one single array.
You cannot do that.
The closest you can get to that is by using std::array
.
std::array<int, 10> arr1 = 1,2,3,4,5,6,7,8,9,10;
...
std::array<int, 10> arr10 = 1,2,3,4,5,6,7,8,9,10;
std::array<std::array<int, 10>, 10> arrayOfArray = arr1, ..., arr10;
add a comment |
Basically, I want to append all 10 of these arrays one after another to make one single array.
You cannot do that.
The closest you can get to that is by using std::array
.
std::array<int, 10> arr1 = 1,2,3,4,5,6,7,8,9,10;
...
std::array<int, 10> arr10 = 1,2,3,4,5,6,7,8,9,10;
std::array<std::array<int, 10>, 10> arrayOfArray = arr1, ..., arr10;
Basically, I want to append all 10 of these arrays one after another to make one single array.
You cannot do that.
The closest you can get to that is by using std::array
.
std::array<int, 10> arr1 = 1,2,3,4,5,6,7,8,9,10;
...
std::array<int, 10> arr10 = 1,2,3,4,5,6,7,8,9,10;
std::array<std::array<int, 10>, 10> arrayOfArray = arr1, ..., arr10;
answered Mar 22 at 5:14
R SahuR Sahu
171k1297195
171k1297195
add a comment |
add a comment |
I want to append all 10 of these arrays one after another to make one
single array ?
You can have array of pointers like
int *ArrayOfPointers[10] = &arr1, &arr2, &arr3, &arr4, &arr5, &arr6, &arr7, &arr8, &arr9, &arr10;
Here ArrayOfPointers
is array of 10
int pointers i.e it can store address of 10
one dimension int array like arr1
, arr2
etc.
I assume there may be better method than what I'm suggesting in advance C++
for the same task.
add a comment |
I want to append all 10 of these arrays one after another to make one
single array ?
You can have array of pointers like
int *ArrayOfPointers[10] = &arr1, &arr2, &arr3, &arr4, &arr5, &arr6, &arr7, &arr8, &arr9, &arr10;
Here ArrayOfPointers
is array of 10
int pointers i.e it can store address of 10
one dimension int array like arr1
, arr2
etc.
I assume there may be better method than what I'm suggesting in advance C++
for the same task.
add a comment |
I want to append all 10 of these arrays one after another to make one
single array ?
You can have array of pointers like
int *ArrayOfPointers[10] = &arr1, &arr2, &arr3, &arr4, &arr5, &arr6, &arr7, &arr8, &arr9, &arr10;
Here ArrayOfPointers
is array of 10
int pointers i.e it can store address of 10
one dimension int array like arr1
, arr2
etc.
I assume there may be better method than what I'm suggesting in advance C++
for the same task.
I want to append all 10 of these arrays one after another to make one
single array ?
You can have array of pointers like
int *ArrayOfPointers[10] = &arr1, &arr2, &arr3, &arr4, &arr5, &arr6, &arr7, &arr8, &arr9, &arr10;
Here ArrayOfPointers
is array of 10
int pointers i.e it can store address of 10
one dimension int array like arr1
, arr2
etc.
I assume there may be better method than what I'm suggesting in advance C++
for the same task.
edited Mar 22 at 5:15
answered Mar 22 at 5:11
AchalAchal
9,5982931
9,5982931
add a comment |
add a comment |
Try this approach:
#include <iostream>
#include <vector>
int arr1[] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
int arr2[] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
// ...other arrays here...
// We pass a reference to a vector and return the same vector for performance reasons.
// Compiler often can optimize that into a better code.
std::vector<int> append(std::vector<int> & vec, int * data, int size)
for (int i = 0; i < size; ++i)
vec.push_back(data[i]);
return vec;
int main()
std::vector<int> data;
data = append(data, arr1, 10);
data = append(data, arr2, 10);
for (auto i : data)
std::cout << i << ", ";
std::cout << std::endl;
return 0;
Also, in C++ there are good containers for storing arrays, try searching for std::array
and std::vector
containers. First is a fixed size static array, the other one is dynamic.
add a comment |
Try this approach:
#include <iostream>
#include <vector>
int arr1[] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
int arr2[] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
// ...other arrays here...
// We pass a reference to a vector and return the same vector for performance reasons.
// Compiler often can optimize that into a better code.
std::vector<int> append(std::vector<int> & vec, int * data, int size)
for (int i = 0; i < size; ++i)
vec.push_back(data[i]);
return vec;
int main()
std::vector<int> data;
data = append(data, arr1, 10);
data = append(data, arr2, 10);
for (auto i : data)
std::cout << i << ", ";
std::cout << std::endl;
return 0;
Also, in C++ there are good containers for storing arrays, try searching for std::array
and std::vector
containers. First is a fixed size static array, the other one is dynamic.
add a comment |
Try this approach:
#include <iostream>
#include <vector>
int arr1[] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
int arr2[] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
// ...other arrays here...
// We pass a reference to a vector and return the same vector for performance reasons.
// Compiler often can optimize that into a better code.
std::vector<int> append(std::vector<int> & vec, int * data, int size)
for (int i = 0; i < size; ++i)
vec.push_back(data[i]);
return vec;
int main()
std::vector<int> data;
data = append(data, arr1, 10);
data = append(data, arr2, 10);
for (auto i : data)
std::cout << i << ", ";
std::cout << std::endl;
return 0;
Also, in C++ there are good containers for storing arrays, try searching for std::array
and std::vector
containers. First is a fixed size static array, the other one is dynamic.
Try this approach:
#include <iostream>
#include <vector>
int arr1[] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
int arr2[] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
// ...other arrays here...
// We pass a reference to a vector and return the same vector for performance reasons.
// Compiler often can optimize that into a better code.
std::vector<int> append(std::vector<int> & vec, int * data, int size)
for (int i = 0; i < size; ++i)
vec.push_back(data[i]);
return vec;
int main()
std::vector<int> data;
data = append(data, arr1, 10);
data = append(data, arr2, 10);
for (auto i : data)
std::cout << i << ", ";
std::cout << std::endl;
return 0;
Also, in C++ there are good containers for storing arrays, try searching for std::array
and std::vector
containers. First is a fixed size static array, the other one is dynamic.
answered Mar 22 at 6:37
DiodacusDiodacus
2276
2276
add a comment |
add a comment |
In C++ it is unnecessary and ill-advised to use C-style arrays. For arrays of
constant size you may use std::array
and for arrays of variable size, std::vector
It looks rather as if what you actually want is a constant two-dimensional matrix
and to be able to access each of its rows as as a constant array, but do not
know how to initialise a two-dimensional matrix. If that's the case, here's how:
#include <iostream>
#include <array>
std::array<std::array<int,10>,10> matrix =
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10
;
int main()
std::array<int,10> const & arr0 = matrix[0];
for (int const & i : arr0)
std::cout << i << ' ';
std::cout << std::endl;
// Or more simply...
auto const & arr5 = matrix[5];
for (auto const & i : arr5)
std::cout << i << ' ';
std::cout << std::endl;
Compile, link and run:
$ g++ -Wall -Wextra main.cpp && ./a.out
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
live demo
add a comment |
In C++ it is unnecessary and ill-advised to use C-style arrays. For arrays of
constant size you may use std::array
and for arrays of variable size, std::vector
It looks rather as if what you actually want is a constant two-dimensional matrix
and to be able to access each of its rows as as a constant array, but do not
know how to initialise a two-dimensional matrix. If that's the case, here's how:
#include <iostream>
#include <array>
std::array<std::array<int,10>,10> matrix =
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10
;
int main()
std::array<int,10> const & arr0 = matrix[0];
for (int const & i : arr0)
std::cout << i << ' ';
std::cout << std::endl;
// Or more simply...
auto const & arr5 = matrix[5];
for (auto const & i : arr5)
std::cout << i << ' ';
std::cout << std::endl;
Compile, link and run:
$ g++ -Wall -Wextra main.cpp && ./a.out
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
live demo
add a comment |
In C++ it is unnecessary and ill-advised to use C-style arrays. For arrays of
constant size you may use std::array
and for arrays of variable size, std::vector
It looks rather as if what you actually want is a constant two-dimensional matrix
and to be able to access each of its rows as as a constant array, but do not
know how to initialise a two-dimensional matrix. If that's the case, here's how:
#include <iostream>
#include <array>
std::array<std::array<int,10>,10> matrix =
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10
;
int main()
std::array<int,10> const & arr0 = matrix[0];
for (int const & i : arr0)
std::cout << i << ' ';
std::cout << std::endl;
// Or more simply...
auto const & arr5 = matrix[5];
for (auto const & i : arr5)
std::cout << i << ' ';
std::cout << std::endl;
Compile, link and run:
$ g++ -Wall -Wextra main.cpp && ./a.out
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
live demo
In C++ it is unnecessary and ill-advised to use C-style arrays. For arrays of
constant size you may use std::array
and for arrays of variable size, std::vector
It looks rather as if what you actually want is a constant two-dimensional matrix
and to be able to access each of its rows as as a constant array, but do not
know how to initialise a two-dimensional matrix. If that's the case, here's how:
#include <iostream>
#include <array>
std::array<std::array<int,10>,10> matrix =
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10
;
int main()
std::array<int,10> const & arr0 = matrix[0];
for (int const & i : arr0)
std::cout << i << ' ';
std::cout << std::endl;
// Or more simply...
auto const & arr5 = matrix[5];
for (auto const & i : arr5)
std::cout << i << ' ';
std::cout << std::endl;
Compile, link and run:
$ g++ -Wall -Wextra main.cpp && ./a.out
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
live demo
edited Mar 22 at 14:43
answered Mar 22 at 12:51
Mike KinghanMike Kinghan
32.9k870119
32.9k870119
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%2f55293155%2fhow-do-i-append-10-arrays-to-form-a-single-array-in-c%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
What do you mean by add all 10 of these arrays to one single array? like
[A]+[B]+...+[J] = [Z]
or[Z] = [[A][B]...[J]]
– RC0993
Mar 22 at 5:00
Like this: ArrayOfArrays = arr1[], arr2[], arr3[], arr4[], arr5[], arr6[], arr7[], arr8[], arr9[], arr10[]
– bdbasinger
Mar 22 at 5:02
So, you want to create a matrix?
– Havenard
Mar 22 at 5:05
Is there a constraint of using arrays? Are the sizes of all arrays pre defined/ preknown? if not then you may use linked list. otherwise if the things are pre defined then its simple to declare a resultant array n populate it
– RC0993
Mar 22 at 5:07
Yes the sizes are predefined. How would I declare a resultant array and populate it?
– bdbasinger
Mar 22 at 5:09