0

I want to create a vector of array

vector<BYTE[6]> cKey;
BYTE keyTemp[6];
memset(keyTemp, 0xFF, sizeof(keyTemp)); // Key is FFF...FF
cKey.push_back(keyTemp);

But in push_back function, that show error

Error C3074: an array can only be initialized with an initializer-list

How to initialize a vector of array ?

Manh Le
  • 13
  • 1
  • 3
  • 10
  • Found a duplicate: http://stackoverflow.com/q/4612273/3093378 – vsoftco Oct 20 '15 at 03:56
  • @Manh Le: Please read the conversation in the `vsoftco` answer and I'm encouraging you to use his way. If not, please use my answer with care and understand what going on. – Nayana Adassuriya Oct 21 '15 at 02:48

2 Answers2

3

You cannot. The underlying type an std::vector uses must be CopyAssignable, and arrays are not. Reference: http://en.cppreference.com/w/cpp/container/vector

A solution is to use a std::array as the underlying type, instead of raw arrays:

#include <iostream>
#include <vector>
#include <array>

using BYTE = unsigned char;

int main()
{
    std::vector<std::array<BYTE, 6>> cKey;
    std::array<BYTE, 6> keyTemp{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
    cKey.push_back(keyTemp);
}
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Cant we consider array as just a pointer. What's wrong with creating vector of pointer? – Nayana Adassuriya Oct 21 '15 at 02:35
  • @NayanaAdassuriya No, because internally the vector does something like `T some_value = T()`, where the type `T` of `some_value` is array. So it tries to assign an array to another array, which is not possible. It's like you trying to do `int arr[10]; int arr_copy[10]; arr_copy = arr; /*error here*/` – vsoftco Oct 21 '15 at 02:36
  • lets say array is just a pointer. it can be assign one pointer to another. But issue is wen it goes out from the function scope, pointer become invalid because original array is not there anymore in the memory. is that correct idea you try to say? – Nayana Adassuriya Oct 21 '15 at 02:41
  • Yes, basically the vector doesn't own its elements. Sometimes this may be what you want, but other times you may want to copy the elements into the vector, in which case a pointer is not the way to go. And of course the pointer has to be valid at least as long as the vector, otherwise the vector will end up with dangling pointers. – vsoftco Oct 21 '15 at 02:44
  • Got the idea, Thank you! I think my answer drag OP in to trouble. Let me correct it. – Nayana Adassuriya Oct 21 '15 at 02:46
  • @NayanaAdassuriya Using pointers is OK when that's the intention, it's not wrong per se. But I believe the OP wanted to copy the array inside the vector, in which case the pointer won't do it. – vsoftco Oct 21 '15 at 02:48
-1

I don't know what data type you used a BYTE. Here how it works with int array.

#include <iostream>
#include <vector>

    int main()
    {
        std::vector<int*> cKey;
        int keyTemp[8] = { 1,2,3,4,5,6,7,8 };
        cKey.push_back(keyTemp);
        std::cout << cKey[0][6] << std::endl;
        system("pause");
        return 0;
    }
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147