Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, with vector<int[2]> can I push_back({someNum1,someNum2})?

I have the vector:

vector<int[2]> storeInventory; //storeInventory[INDEX#]{ITEMNUM, QUANTITY}

and I am wanting to use the push_back() method to add new arrays to the inventory vector. Something similar to this:

const int ORANGE = 100001;
const int GRAPE = 100002

storeInventory.push_back({GRAPE,24});
storeInventory.push_back{ORANGE, 30};

However, when I try using the syntax as I have above I get the error Error: excpeted an expression. Is what I am trying just not possible, or am I just going about it the wrong way?

like image 866
Brook Julias Avatar asked Nov 30 '12 19:11

Brook Julias


2 Answers

Built-in arrays are not Assignable or CopyConstructible. This violates container element requirements (at least for C++03 and earlier). In other words, you can't have std::vector of int[2] elements. You have to wrap your array type to satisfy the above requirements.

As it has already been suggested, std::array in a perfect candidate for a wrapper type in C++11. Or you can just do

struct Int2 {
  int a[2];
};

and use std::vector<Int2>.

like image 198
AnT Avatar answered Sep 21 '22 12:09

AnT


I don't believe it's possible to pass arrays like that. Consider using std::array instead:

vector<std::array<int, 2> > storeInventory; 
storeInventory.push_back({{GRAPE,24}});
like image 38
Pubby Avatar answered Sep 23 '22 12:09

Pubby