4

I'm currently attempting to create a vector that holds float arrays. I am having a diffcult time.

I have the following code:

float testArray[4] = {20, -3.14/2, 5, -3.14/2};
std::vector<float[4]> inputVector;
std::vector<float[4]>::iterator it = inputVector.begin();
inputVector.insert(it, testArray);

I keep getting an error say "array must be initialized with a brace-enclosed initializer" and "invalid array assignment". I tried this same code with a vector of ints (as opposed to a vector of array) and did not have any issue.

I believe there is an underlying issue that I don't understand.

Any help is appreciated!

Izzo
  • 4,461
  • 13
  • 45
  • 82
  • 1
    Arrays can not be copied with normal copy-semantics (or moved since C++11), and vectors require that the stored data type is. See e.g. [this `std::vector` reference](http://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Nov 14 '15 at 18:39
  • Possible duplicate of [What is the easiest way to initialize a std::vector with hardcoded elements?](http://stackoverflow.com/questions/2236197/what-is-the-easiest-way-to-initialize-a-stdvector-with-hardcoded-elements) – AndersK Nov 14 '15 at 19:00

2 Answers2

7

Use std::array. C-style arrays shouldn't generally be used in modern C++ code.

#include <vector>
#include <array>

int main()
{
  std::array<float, 4> testArray{{20, -3.14/2, 5, -3.14/2}};
  std::vector<std::array<float, 4>> inputVector;
  std::vector<std::array<float, 4>>::iterator it = inputVector.begin();
  inputVector.insert(it, testArray);
}

Read these questions/answers for more information:


Note that this will copy the array and all its contents. If you want to refer to the existing testArray instance, create an std::vector<std::array<float, 4>*> and take the address of the instance when inserting it in the vector: inputVector.insert(it, &testArray);.

Community
  • 1
  • 1
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • @tobi303: I'm using `std::array` in place of `float[4]`, not in place of the `std::vector`. C-style arrays have no use in modern C++ code. – Vittorio Romeo Nov 14 '15 at 18:41
  • ups sorry, I was a bit fast with posting nonsense ;) – 463035818_is_not_an_ai Nov 14 '15 at 18:43
  • @VittorioRomeo what about when you are transmitting arrays over something like a serial link? Can I use C-style arrays then? – Sam Oct 07 '16 at 19:07
  • @Sam: why wouldn't you be able to use `std::array` in that situation? – Vittorio Romeo Oct 07 '16 at 19:46
  • @VittorioRomeo Because you on have the std::array.data you don't have any of the ancillary information that the array class stores (size comes to mind). – Sam Oct 07 '16 at 20:57
  • @Sam: [yes you have](http://en.cppreference.com/w/cpp/container/array/size) - just pass `(array.data(), array.size())` to the function that requires a C-style interface. – Vittorio Romeo Oct 10 '16 at 08:10
1

How about this :-

float t[4] = {20, -3.14/2, 5, -3.14/2};
float *testArray=t;
std::vector<float*> inputVector;
std::vector<float*>::iterator it = inputVector.begin();
inputVector.insert(it, testArray);

It would be better if you use std::array<float, 4> instead of in-built arrays. The problem with your previous code was that you were attempting something like :-

float a[4] {1.1, 2.2, 3.3, 4.4};
float b[4] = a;   // illegal conversion of float[] to float*

Hence your code used to fail

Ankit Acharya
  • 2,833
  • 3
  • 18
  • 29