New answers tagged multidimensional-array
Score of 1
1
vote
Initialization of all elements of a multidimensional std::array to one default value
For fixed‑size multidimensional arrays, std::array works fine, but it quickly becomes cumbersome to manipulate and initialize.
A simple alternative is to linearize the 2D structure into a single ...
Score of 0
0
votes
Initialization of all elements of a multidimensional std::array to one default value
If you can switch to the underlying storage to a 1d array you can use a mdspan as 2d view. The 1d arrays elements can be easily initialized via the arrays fill:
#include <iostream>
#include <...
Score of 3
3
votes
Initialization of all elements of a multidimensional std::array to one default value
Pitfall of "it's only two lines"
You have discovered one kind of difficulty that can arise from not modularizing your code. Yes, your initialization is "only" two lines, but, as ...
Score of 1
1
vote
Initialization of all elements of a multidimensional std::array to one default value
You can write a helper template that creates and returns the 2D array with prefilled values as shown below: Live demo
#include <print>
#include <array>
//Helper template that creates and ...
Score of -1
-1
votes
Initialization of all elements of a multidimensional std::array to one default value
Multidimensional contiguous array is a way more complicated concept than meets the eye that's why C++ is still struggling with standardizing a certain interface for it. The fixed sized std::array ...
Score of 4
4
votes
Initialization of all elements of a multidimensional std::array to one default value
std::array is an aggregate so you'd have to explicitly spell it out to actually initialize it with the values you want:
std::array<std::array<double, 2>, 3> arr{{
{1., 2.},
{3., 4.}...
Score of 3
3
votes
Initialization of all elements of a multidimensional std::array to one default value
A straightforward and primitive approach is for example to use the range-based for loop.
#include <iostream>
#include <array>
int main()
{
std::array<std::array<double, 2>, 3&...
Score of 2
2
votes
Initialization of all elements of a multidimensional std::array to one default value
There isn't a built-in one-liner the way std::vector's constructor gives you, because std::array's size is fixed at compile time and it has no "fill constructor" -- you can only default-...
Score of 0
0
votes
Merge two arrays and continue to increase row ids to avoid numeric gaps
Since the id values in both arrays are 1-based, you can just add the number of rows in the first array to the id values in the second array and push mutated rows from the second array onto the end of ...
Score of 0
0
votes
Group form submission payload by prefix of all keys
All of your key prefixes are words, so you can simply rely on strstr() to isolate the substring that precedes the first occuring underscore in the key or if there is no underscore, use the whole key. ...
Score of 0
0
votes
How do get the differences of a multi dimensional array that have different string keys?
For simplicity, loop over the stored array and check for related keys in the deletion array. If there are related data sets, compare them with array_diff() and save the result back to the original ...
Score of 5
5
votes
Logic behind sizeof() for 2D arrays in C
In general if you have an array declared like
T a[] = { /* some initializing list */ };
where T is some arbitrary type then to calculate the actual number of elements stored in the array you can ...
Score of 7
7
votes
Logic behind sizeof() for 2D arrays in C
How to compute the number of elements in an array
The number of elements in an array A is sizeof A / sizeof A[0] because:
For any array A:
A[0] is the first element of the array.
sizeof A is the ...
Score of 7
7
votes
Logic behind sizeof() for 2D arrays in C
In C, the sizeof operator is used to determine the size of an object in bytes.
In your case, to find the number of elements in the array, you take the size of the entire array and divide it by the ...
Score of 7
7
votes
Logic behind sizeof() for 2D arrays in C
sizeof fruits (which is equivalent to sizeof(fruits)) is the size in bytes of the entire array fruits, which is 30 in the posted example.
sizeof fruits[0] (which is equivalent to sizeof(fruits[0])) is ...
Score of 10
10
votes
Accepted
Logic behind sizeof() for 2D arrays in C
This usage isn't particular to 2D arrays, but to arrays in general.
The expression sizeof(fruits) gives the total size in bytes of the fruits array in bytes. Similarly, sizeof(fruits[0]) gives the ...
Score of 4
4
votes
Replace element in array
foreach ($aCount as $key => $value) { $aProductsExploded[$key][1] = $value; }
should do the trick to replace the values in your helper array $aProductsExploded.
The whole thing could look ...
Top 50 recent answers are included
Related Tags
multidimensional-array × 36191arrays × 19371
php × 11596
java × 4284
python × 3714
javascript × 3545
c × 3145
c++ × 2717
numpy × 2109
sorting × 1628
c# × 1570
json × 1089
pointers × 1035
matrix × 935
for-loop × 781
foreach × 722
mysql × 711
loops × 710
jquery × 624
list × 615
matlab × 594
recursion × 537
string × 534
algorithm × 516
arraylist × 503