Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Stack of int Arrays

Tags:

c++

arrays

stack

I'm trying to make a stack of integer arrays, like so:

stack<int[2]> stk;

int arr[2] = {1,2};
stk.push(arr);

however, Visual C++ gives me the error

error C2075: 'Target of operator new()' : array initialization needs curly braces

and MinGW gives me the error

error: parenthesized initializer in array new

The error seems to be coming from stk.push(arr). What does the error mean, and how would I properly make a stack of integer arrays?

like image 942
woojoo666 Avatar asked Jan 23 '26 14:01

woojoo666


1 Answers

With C++11 arrays you can do this:

#include <stack>
#include <array>

stack<array<int, 2>> arrs;
arrs.push({1, 2});

As mentioned in a comment to the question, it is also possible to replace array<int, 2> with vector<int>. However, array<int, 2> achieves what you where describing with a fixed size container (and lower memory usage.)

like image 67
Lasse Espeholt Avatar answered Jan 25 '26 03:01

Lasse Espeholt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!