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
-1
votes
4 answers

Range based for with multi-dimensional std::array

Tried looking around for this but only found examples using built in array not std::array obj. // array arr of size 5 array< array, 10> arr = { 0 }; srand((unsigned)time(0)); // initialize elements for () { for() { item =…
Alvaromon
  • 190
  • 2
  • 16
-1
votes
2 answers

How should I construct a smaller std::array as a slice of a larger one?

I want to represent elements i through i+k-1 of an std::array as another std::array, for constexpr i and k - preferably with no copying and in a constexpr function. Can this be done? If so, what's the right way to approach this?
einpoklum
  • 118,144
  • 57
  • 340
  • 684
-2
votes
1 answer

Is it possible to use values in an array for implicit aggregate initialization/assignment of a struct?

I have a struct struct RGB { int r; int g; int b; } I know that aggregate initialization can be used: RGB rgb = {255, 255, 255}; I wonder if it is possible to assign an array to it. I've tried these solutions and they do not work: int…
-2
votes
1 answer

question about bounds checking inside standard containers

i have written a function that returns the data at an index for a simple implementation of an array type similar to std::array. constexpr const T& at(size_t index) const { if(index < Size) return data_[index]; throw std::out_of_range ("Index…
Adrian Costin
  • 418
  • 3
  • 12
-2
votes
1 answer

Cannot create constructor with std::array as class member

Here the code #include "card.h" #include constexpr int DECK_SIZE = 52; class Deck { std::array m_deck; public: Deck(); ~Deck() = default; Card getCard(int index) { return m_deck[index]; } }; Card is just…
JohnDoe
  • 179
  • 1
  • 4
  • 17
-2
votes
2 answers

What's wrong in this snippet?

I have no idea what's wrong with this snippet. I'm getting this error: error: member function 'swap' not viable: 'this' argument has type 'const array', but function is not marked const #include #include #include…
Dean
  • 6,610
  • 6
  • 40
  • 90
-3
votes
1 answer

Strange outputs of std::to_string in C++11

I have this small snippet of C++ code: #include #include #include int main() { std::string name = "mario"; std::cerr << "Hello world! " + name + "\n"; std::array arr = {12, 12.3, 13, 14}; std::cerr <<…
kmario23
  • 57,311
  • 13
  • 161
  • 150
-4
votes
3 answers

Why there is no operator-> in std::array

I am writing a std::array class as a little exercise, during which I found out that std::array does not implement operator->. Thus for arrays the following is allowed struct S { int s; }; S c[2]; c->s = 2; but for std::array this is not…
tommsch
  • 582
  • 4
  • 19
-4
votes
1 answer

Why did STL made a(nother) distinction between a std::array and a std::initializer_list

Why is there not only 1 type with the best performance, to define a list at compile time? The benchmark is clear, the constexpr std::initializer_list is faster, uses even less memory and the IO-reads are astonishingly less over constexpr…
jaques-sam
  • 2,578
  • 1
  • 26
  • 24
-4
votes
2 answers

Initialize map with pair of std::arrays c++11

I would like to compile this lines. Insert to map pair of std::arrays. #include #include #include #include using namespace std; int main() { array l; array r; map
andrew
  • 3,083
  • 4
  • 24
  • 29
-4
votes
2 answers

2-Dimensional std::array woes

I thought I liked the idea of std::array from C++11, but it looks like it has some quarks. I found this out because the following code gives a segmentation fault: #include #include int main() { std::array
Sammaron
  • 196
  • 1
  • 3
  • 14
-5
votes
1 answer

How to fix compiler error when try to initialise a std::array from a non const size

I know I can use a vector, but I don't need the extra features of a vector so wanted to use a std::array. The problem I have is that I get the size of the array from the user - from stdin. Then I know the size and initialise the array from…
Angus Comber
  • 9,316
  • 14
  • 59
  • 107
-9
votes
1 answer

What variations of vector-like containers already widely established? Do I have to write my own?

In my program, I often need an owning array-like container - i.e. for data stored contiguously in memory, but vector is too flexible and less practical or efficient than it could be. The requirements differ from std::vector in one or more aspects…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1 2 3
28
29