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
148
votes
6 answers

Passing a std::array of unknown size to a function

In C++11, how would I go about writing a function (or method) that takes a std::array of known type but unknown size? // made up example void mulArray(std::array& arr, const int multiplier) { for(auto& e : arr) { e *=…
Adrian
  • 2,276
  • 2
  • 19
  • 25
145
votes
7 answers

std::array vs array performance

If I want to build a very simple array like: int myArray[3] = {1,2,3}; Should I use std::array instead? std::array a = {{1, 2, 3}}; What are the advantages of using std::array over usual ones? Is it more performant? Just easier to handle…
Arcyno
  • 4,153
  • 3
  • 34
  • 52
145
votes
5 answers

Default initialization of std::array?

With C++11 std::array, do I have the guarantee that the syntax std::array x; will default-initialize all the elements of the array ? EDIT: if not, is there a syntax that will work on all arrays (including zero-sized arrays) to initialize all…
Vincent
  • 57,703
  • 61
  • 205
  • 388
88
votes
5 answers

Why does std::array not have an constructor that takes a value for the array to be filled with?

Is the absence of std::array::array(const T& value); an oversight? It seems mighty useful to me, and dynamic containers (like std::vector) do have a similar constructor. I am fully aware of std::array::fill(const T& value); but…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
62
votes
2 answers

How to create std::array with initialization list without providing size directly

How can I make a3 compile? int main() { int a1[] = { 1, 2, 3 }; std::array a2 = { 1, 2, 3 }; std::array a3 = { 1, 2, 3 }; } It's very inconvenient, and brittle, to hard-code the size of the array when using an…
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
57
votes
6 answers

Get size of std::array without an instance

Given this struct: struct Foo { std::array bar; }; How can I get the number of elements of the bar array if I don't have an instance of Foo?
ChronoTrigger
  • 8,459
  • 1
  • 36
  • 57
49
votes
4 answers

What is the memory layout of vector of arrays?

can anybody explaine the memory layout of std::vector> vec(2) does it provide contiguous memory block of a 2D array with 2 rows of 5 elements? To my understanding, the vector of vectors std::vector> vec(2,…
Const
  • 1,306
  • 1
  • 10
  • 26
47
votes
8 answers

Initializing a std::array with a constant value

I need to initialize all elements of a std::array with a constant value, like it can be done with std::vector. #include #include int main() { std::vector v(10, 7); // OK std::array a(7); // does not…
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
47
votes
4 answers

Is there a reason for zero sized std::array in C++11?

Consider the following piece of code, which is perfectly acceptable by a C++11 compiler: #include #include auto main() -> int { std::array A; for(auto i : A) std::cout << i << std::endl; return 0; } According…
101010
  • 41,839
  • 11
  • 94
  • 168
41
votes
3 answers

Convenient way to declare 2D (or even higher dimension) arrays with std::array

I'm about to convert a lot of old C++ code to more modern C++. There are many raw 2D arrays in that code like: Foo bar[XSIZE][YSIZE]; And I'm about to replace these declarations with std::array, XSIZE> bar; This is a…
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
40
votes
3 answers

Initialize an std::array algorithmically at compile time

Consider: static constexpr unsigned num_points{ 7810 }; std::array< double, num_points > axis; for (int i = 0; i < num_points; ++i) { axis[i] = 180 + 0.1 * i; } axis is a class-wide constant. I want to avoid initializing it like any other…
Vorac
  • 8,726
  • 11
  • 58
  • 101
35
votes
2 answers

How should I brace-initialize an std::array of std::pairs?

std::array, 2> ids = { { 0, 1 }, { 1, 2 } }; VS2013 error: error C2440: 'initializing' : cannot convert from 'int' to 'std::pair' No constructor could take the source type, or constructor overload resolution was…
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
35
votes
1 answer

Does using std::array lead to code bloat?

I have seen in a few places the recommendation to use std::array over C-style arrays in C++, claiming it is a better, safer alternative with no overhead. See: The standard container array [...] has no space overheads beyond what it needs to hold…
frangio
  • 855
  • 7
  • 18
29
votes
1 answer

Call to implicitly-deleted default constructor

I get the error message Call to implicitly-deleted default constructor of 'std::array' when I try to compile my C++ project. Header file cubic_patch.hpp #include class Point3D{ public: Point3D(float, float, float); private: float…
Itsbananas
  • 569
  • 1
  • 4
  • 12
28
votes
1 answer

Is it undefined behavior to iterate over an std::array initialized in the for-loop?

// This snippet for (const float t : std::array{ 0.0f, 0.33f, 0.66f, 1.0f }) { std::cout << "t = " << t << "\n"; } // Yields the following (incorrect) values: t = -3.91649e-28 t = 4.59037e-41 t = 2.66247e-44 t = 0 // Whereas this snippet ... auto…
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
1
2 3
28 29