Questions tagged [stdarray]

std::array is a container that encapsulates constant size arrays in C++.

std::array is a part of the C++ Standard Library since C++11.

Reference: https://en.cppreference.com/w/cpp/container/array

433 questions
15
votes
1 answer

Initialisation of std::array<>

Consider the following code: #include struct A { int a; int b; }; static std::array x1 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; static std::array x2 = { { { 1, 2 }, …
Jeremy
  • 5,055
  • 1
  • 28
  • 44
14
votes
0 answers

why does creating a 2D array with C++ std::array require an extra pair of {}

To create a 2D array with std::vector, you'd do vector> array2d = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; The outer {} represent the outer vector; the inner {},…
codepro
  • 283
  • 3
  • 8
14
votes
3 answers

Declaring 2 (or even multi-) dimensional std::arrays elegantly

I'm using 2-dimensional arrays based on std::array. Basically instead of: MyType myarray[X_SIZE][Y_SIZE]; I have: std::array, X_SIZE> myarray; This works perfectly fine but IMO the declaration is not very readable. Is…
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
13
votes
2 answers

No error for negative-size array

Why don't I get an error trying to create a negative-size array? #include int main() { std::array arr; } With -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC I get no error. Is this intended behavior?
user6348851
  • 139
  • 1
  • 3
13
votes
3 answers

Extract range of elements from char array into string

I want to extract a range of elements from the beginning of a char array and put them into a string. The range may be less than or equal to the number of elements. This is what I have come up with. // buffer is a std::array std::string…
ksl
  • 4,519
  • 11
  • 65
  • 106
13
votes
1 answer

What is the use of 0-length array (or std::array)?

In C++11 it allows you to create a 0 length C array and std:array like this: int arr1[0]; std::array arr2; So I'm thinking what is the use of a array that doesn't have a space to store? Secondly what is the zero length array? If it is a…
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
13
votes
1 answer

What is the sizeof std::array?

What does the C++ standard say about what sizeof(std::array) should be (for some constant N)? In a comment to a different question, it was mentioned that std::array is not always "stack allocated". The comment was in response to a different…
jxh
  • 69,070
  • 8
  • 110
  • 193
12
votes
1 answer

std::to_array for multi dimensional array

C++20 added std::to_array so you can easily create std::array from a C-Style array, for example: template void foo(const T (&a)[N]) { auto arr = std::to_array(a); } But, std::to_array does not support two-dimensional…
Amir Kirsh
  • 12,564
  • 41
  • 74
12
votes
5 answers

Example where std::array::max_size and std::array::size gives different result

Whenever I try with max_size() and size() funtion of std::array, I get same results, I wanted to know if there could be a situation where two of them give different results.
user1718009
  • 303
  • 3
  • 7
12
votes
1 answer

Initializing an std::array of non-default-constructible elements?

Suppose type foo_t with a named constructor idiom, make_foo(). Now, I want to have exactly 123 foo's - no more, no less. So, I'm thinking about an std::array. Now, if foo_t were default-constructible, I would write: std::array
einpoklum
  • 118,144
  • 57
  • 340
  • 684
12
votes
3 answers

Why isn't the operator[] of a std::array temporary constexpr?

I was stuffing some values into a constexpr std::array and then continuing the compile-time static goodness into more constexpr values when I discovered that you can't use an element as a constexpr initializer in C++11. This is because…
Xo Wang
  • 377
  • 1
  • 4
  • 9
12
votes
4 answers

In C++, how to iterate array in reverse using for_each?

In C++11, using lambda/for_each, how do we iterate an array from end? I tried the following, but both result in infinite loop: for_each (end(A), begin(A), [](int i) { .... }); for_each (A.rend(), A.rbegin(), [](int i) { ... }); Any idea?…
user350954
  • 329
  • 2
  • 3
  • 11
11
votes
1 answer

Is the address of a std::array guaranteed the same as its data?

std::array is ... (quoting from cppreference): This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member. Does that imply that the address of an array is always the same…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
11
votes
1 answer

Fixed-size std::span vs std::array

C++20 includes std::span, which "describes an object that can refer to a contiguous sequence of objects with the first element of the sequence at position zero". Its interface is very close to std::array, though it supports dynamic extent as well as…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
11
votes
3 answers

Is there a way to enforce full initialization of std::array

I am using std::array (N is a fixed template-variable). #include template struct A{ size_t function(std::array arr){ return arr[N-1];} // just an example }; int main(){ A<5> a; …
alfC
  • 14,261
  • 4
  • 67
  • 118
1 2
3
28 29