Declaring 2 (or even multi-) dimensional std::arrays elegantlyHow do I use arrays in C++?Why is reading lines from stdin much slower in C++ than Python?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionHow to avoid “if” chains?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsstd::array and bytes readingSort 2 dimensional c array with std::sort
Plotting level sets of the form f(x,y,c)==0
One hour 10 min layover in Newark; International -> Domestic connection. Enough time to clear customs?
In mathematics is there a substitution that is "different" from Vieta's substitution to solve the cubic equation?
Sum of Infinite series with a Geometric series in multiply
co-son-in-law or co-brother
Why do modes sound so different, although they are basically the same as a mode of another scale?
Which is the best password hashing algorithm in .NET Core?
Why not use futuristic pavise ballistic shields for protection?
Punishment in pacifist society
Strange LockTime values in Electrum transactions?
Meaning of "educating the ice"
How do you manage to study and have a balance in your life at the same time?
How can I oppose my advisor granting gift authorship to a collaborator?
What is the most likely cause of short, quick, and useless reviews?
Ideal characterization of almost convergence
What happens if you just start drawing from the Deck of Many Things without declaring any number of cards?
Does secure hashing imply secure symmetric encryption?
Is torque as fundamental a concept as force?
Map a function that takes arguments in different levels of a list
Using GNU screen, I get raw prompt with backslashes
Can my UK debt be collected because I have to return to US?
What is the significance of 104%
Why do old games use flashing as means of showing damage?
Is mathematics truth?
Declaring 2 (or even multi-) dimensional std::arrays elegantly
How do I use arrays in C++?Why is reading lines from stdin much slower in C++ than Python?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionHow to avoid “if” chains?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsstd::array and bytes readingSort 2 dimensional c array with std::sort
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm using 2-dimensional arrays based on std::array.
Basically instead of:
MyType myarray[X_SIZE][Y_SIZE];
I have:
std::array<std::array<MyType, Y_SIZE>, X_SIZE> myarray;
This works perfectly fine but IMO the declaration it is not very readable.
Is there a way to declare this using some clever C++ template mecanism, so the declaration could look something like this?
My2DArray<Mytype, X_SIZE, Y_SIZE> myarray;
c++ stdarray
add a comment |
I'm using 2-dimensional arrays based on std::array.
Basically instead of:
MyType myarray[X_SIZE][Y_SIZE];
I have:
std::array<std::array<MyType, Y_SIZE>, X_SIZE> myarray;
This works perfectly fine but IMO the declaration it is not very readable.
Is there a way to declare this using some clever C++ template mecanism, so the declaration could look something like this?
My2DArray<Mytype, X_SIZE, Y_SIZE> myarray;
c++ stdarray
1
I don't think code elegance was/is a priority of 'Modern' C++!
– Adrian
10 hours ago
I would expect it to be[Y][X](andstd::array<std::array<T, X>, Y>in the template in the answer), as usually I think about X being the base dimension, which is iterated mostly continuously.. (of course if you iterate mostly through Y, then your example is optimal too, so it depends on the usage)
– Ped7g
10 hours ago
@Ped7g no, X,Y rather than Y,X is on purpose because accessing is done like this:myarray[x_index][y_index].
– Jabberwocky
9 hours ago
@Jabberwocky that's what I am talking about, if it's for exampleint map[4][3];then the memory layout is[0][0], [0][1], [0][2], [1][0], [1][1], ..., i.e. the second dimension form continuous blocks of integers, so you should (when possible) iterate over second dimension in the inner loop, and first dimension in the outer loop. In your dimension that means to have X as outer loop and Y inner (I mean for cases where this distinction is even possible, like adding of matrices, in multiplying it doesn't matter that much, as one of the two matrices has to be traversed in suboptimal order), etc
– Ped7g
9 hours ago
@Ped7g it's not about optimisation here but solely about code readability. But you are right.
– Jabberwocky
9 hours ago
add a comment |
I'm using 2-dimensional arrays based on std::array.
Basically instead of:
MyType myarray[X_SIZE][Y_SIZE];
I have:
std::array<std::array<MyType, Y_SIZE>, X_SIZE> myarray;
This works perfectly fine but IMO the declaration it is not very readable.
Is there a way to declare this using some clever C++ template mecanism, so the declaration could look something like this?
My2DArray<Mytype, X_SIZE, Y_SIZE> myarray;
c++ stdarray
I'm using 2-dimensional arrays based on std::array.
Basically instead of:
MyType myarray[X_SIZE][Y_SIZE];
I have:
std::array<std::array<MyType, Y_SIZE>, X_SIZE> myarray;
This works perfectly fine but IMO the declaration it is not very readable.
Is there a way to declare this using some clever C++ template mecanism, so the declaration could look something like this?
My2DArray<Mytype, X_SIZE, Y_SIZE> myarray;
c++ stdarray
c++ stdarray
asked 10 hours ago
JabberwockyJabberwocky
29.8k10 gold badges43 silver badges78 bronze badges
29.8k10 gold badges43 silver badges78 bronze badges
1
I don't think code elegance was/is a priority of 'Modern' C++!
– Adrian
10 hours ago
I would expect it to be[Y][X](andstd::array<std::array<T, X>, Y>in the template in the answer), as usually I think about X being the base dimension, which is iterated mostly continuously.. (of course if you iterate mostly through Y, then your example is optimal too, so it depends on the usage)
– Ped7g
10 hours ago
@Ped7g no, X,Y rather than Y,X is on purpose because accessing is done like this:myarray[x_index][y_index].
– Jabberwocky
9 hours ago
@Jabberwocky that's what I am talking about, if it's for exampleint map[4][3];then the memory layout is[0][0], [0][1], [0][2], [1][0], [1][1], ..., i.e. the second dimension form continuous blocks of integers, so you should (when possible) iterate over second dimension in the inner loop, and first dimension in the outer loop. In your dimension that means to have X as outer loop and Y inner (I mean for cases where this distinction is even possible, like adding of matrices, in multiplying it doesn't matter that much, as one of the two matrices has to be traversed in suboptimal order), etc
– Ped7g
9 hours ago
@Ped7g it's not about optimisation here but solely about code readability. But you are right.
– Jabberwocky
9 hours ago
add a comment |
1
I don't think code elegance was/is a priority of 'Modern' C++!
– Adrian
10 hours ago
I would expect it to be[Y][X](andstd::array<std::array<T, X>, Y>in the template in the answer), as usually I think about X being the base dimension, which is iterated mostly continuously.. (of course if you iterate mostly through Y, then your example is optimal too, so it depends on the usage)
– Ped7g
10 hours ago
@Ped7g no, X,Y rather than Y,X is on purpose because accessing is done like this:myarray[x_index][y_index].
– Jabberwocky
9 hours ago
@Jabberwocky that's what I am talking about, if it's for exampleint map[4][3];then the memory layout is[0][0], [0][1], [0][2], [1][0], [1][1], ..., i.e. the second dimension form continuous blocks of integers, so you should (when possible) iterate over second dimension in the inner loop, and first dimension in the outer loop. In your dimension that means to have X as outer loop and Y inner (I mean for cases where this distinction is even possible, like adding of matrices, in multiplying it doesn't matter that much, as one of the two matrices has to be traversed in suboptimal order), etc
– Ped7g
9 hours ago
@Ped7g it's not about optimisation here but solely about code readability. But you are right.
– Jabberwocky
9 hours ago
1
1
I don't think code elegance was/is a priority of 'Modern' C++!
– Adrian
10 hours ago
I don't think code elegance was/is a priority of 'Modern' C++!
– Adrian
10 hours ago
I would expect it to be
[Y][X] (and std::array<std::array<T, X>, Y> in the template in the answer), as usually I think about X being the base dimension, which is iterated mostly continuously.. (of course if you iterate mostly through Y, then your example is optimal too, so it depends on the usage)– Ped7g
10 hours ago
I would expect it to be
[Y][X] (and std::array<std::array<T, X>, Y> in the template in the answer), as usually I think about X being the base dimension, which is iterated mostly continuously.. (of course if you iterate mostly through Y, then your example is optimal too, so it depends on the usage)– Ped7g
10 hours ago
@Ped7g no, X,Y rather than Y,X is on purpose because accessing is done like this:
myarray[x_index][y_index].– Jabberwocky
9 hours ago
@Ped7g no, X,Y rather than Y,X is on purpose because accessing is done like this:
myarray[x_index][y_index].– Jabberwocky
9 hours ago
@Jabberwocky that's what I am talking about, if it's for example
int map[4][3]; then the memory layout is [0][0], [0][1], [0][2], [1][0], [1][1], ..., i.e. the second dimension form continuous blocks of integers, so you should (when possible) iterate over second dimension in the inner loop, and first dimension in the outer loop. In your dimension that means to have X as outer loop and Y inner (I mean for cases where this distinction is even possible, like adding of matrices, in multiplying it doesn't matter that much, as one of the two matrices has to be traversed in suboptimal order), etc– Ped7g
9 hours ago
@Jabberwocky that's what I am talking about, if it's for example
int map[4][3]; then the memory layout is [0][0], [0][1], [0][2], [1][0], [1][1], ..., i.e. the second dimension form continuous blocks of integers, so you should (when possible) iterate over second dimension in the inner loop, and first dimension in the outer loop. In your dimension that means to have X as outer loop and Y inner (I mean for cases where this distinction is even possible, like adding of matrices, in multiplying it doesn't matter that much, as one of the two matrices has to be traversed in suboptimal order), etc– Ped7g
9 hours ago
@Ped7g it's not about optimisation here but solely about code readability. But you are right.
– Jabberwocky
9 hours ago
@Ped7g it's not about optimisation here but solely about code readability. But you are right.
– Jabberwocky
9 hours ago
add a comment |
1 Answer
1
active
oldest
votes
If you want just 2D arrays, it's fairly straightforward:
template <class T, std::size_t X, std::size_t Y>
using My2DArray = std::array<std::array<T, Y>, X>;
If you want a generic mechanism not limited to 2D arrays, it can be done too:
template <class T, std::size_t N, std::size_t... Ns>
struct AddArray
using type = std::array<typename AddArray<T, Ns...>::type, N>;
;
template <class T, std::size_t N>
struct AddArray<T, N>
using type = std::array<T, N>;
;
template <class T, std::size_t... N>
using MyNDArray = typename AddArray<T, N...>::type;
[Live example]
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%2f57758589%2fdeclaring-2-or-even-multi-dimensional-stdarrays-elegantly%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
If you want just 2D arrays, it's fairly straightforward:
template <class T, std::size_t X, std::size_t Y>
using My2DArray = std::array<std::array<T, Y>, X>;
If you want a generic mechanism not limited to 2D arrays, it can be done too:
template <class T, std::size_t N, std::size_t... Ns>
struct AddArray
using type = std::array<typename AddArray<T, Ns...>::type, N>;
;
template <class T, std::size_t N>
struct AddArray<T, N>
using type = std::array<T, N>;
;
template <class T, std::size_t... N>
using MyNDArray = typename AddArray<T, N...>::type;
[Live example]
add a comment |
If you want just 2D arrays, it's fairly straightforward:
template <class T, std::size_t X, std::size_t Y>
using My2DArray = std::array<std::array<T, Y>, X>;
If you want a generic mechanism not limited to 2D arrays, it can be done too:
template <class T, std::size_t N, std::size_t... Ns>
struct AddArray
using type = std::array<typename AddArray<T, Ns...>::type, N>;
;
template <class T, std::size_t N>
struct AddArray<T, N>
using type = std::array<T, N>;
;
template <class T, std::size_t... N>
using MyNDArray = typename AddArray<T, N...>::type;
[Live example]
add a comment |
If you want just 2D arrays, it's fairly straightforward:
template <class T, std::size_t X, std::size_t Y>
using My2DArray = std::array<std::array<T, Y>, X>;
If you want a generic mechanism not limited to 2D arrays, it can be done too:
template <class T, std::size_t N, std::size_t... Ns>
struct AddArray
using type = std::array<typename AddArray<T, Ns...>::type, N>;
;
template <class T, std::size_t N>
struct AddArray<T, N>
using type = std::array<T, N>;
;
template <class T, std::size_t... N>
using MyNDArray = typename AddArray<T, N...>::type;
[Live example]
If you want just 2D arrays, it's fairly straightforward:
template <class T, std::size_t X, std::size_t Y>
using My2DArray = std::array<std::array<T, Y>, X>;
If you want a generic mechanism not limited to 2D arrays, it can be done too:
template <class T, std::size_t N, std::size_t... Ns>
struct AddArray
using type = std::array<typename AddArray<T, Ns...>::type, N>;
;
template <class T, std::size_t N>
struct AddArray<T, N>
using type = std::array<T, N>;
;
template <class T, std::size_t... N>
using MyNDArray = typename AddArray<T, N...>::type;
[Live example]
edited 9 hours ago
answered 10 hours ago
AngewAngew
142k13 gold badges284 silver badges372 bronze badges
142k13 gold badges284 silver badges372 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f57758589%2fdeclaring-2-or-even-multi-dimensional-stdarrays-elegantly%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
1
I don't think code elegance was/is a priority of 'Modern' C++!
– Adrian
10 hours ago
I would expect it to be
[Y][X](andstd::array<std::array<T, X>, Y>in the template in the answer), as usually I think about X being the base dimension, which is iterated mostly continuously.. (of course if you iterate mostly through Y, then your example is optimal too, so it depends on the usage)– Ped7g
10 hours ago
@Ped7g no, X,Y rather than Y,X is on purpose because accessing is done like this:
myarray[x_index][y_index].– Jabberwocky
9 hours ago
@Jabberwocky that's what I am talking about, if it's for example
int map[4][3];then the memory layout is[0][0], [0][1], [0][2], [1][0], [1][1], ..., i.e. the second dimension form continuous blocks of integers, so you should (when possible) iterate over second dimension in the inner loop, and first dimension in the outer loop. In your dimension that means to have X as outer loop and Y inner (I mean for cases where this distinction is even possible, like adding of matrices, in multiplying it doesn't matter that much, as one of the two matrices has to be traversed in suboptimal order), etc– Ped7g
9 hours ago
@Ped7g it's not about optimisation here but solely about code readability. But you are right.
– Jabberwocky
9 hours ago