Reputation: 5474
I have to work on several functions that need to have as argument a stack of arrays.
This is the example:
using namespace std;
typedef vector<array<array<short,2>,64>> dmat;
typedef stack<array<short,2>> lifo;
typedef array<short,2> array2;
array2 posible(lifo& pila, int j){ // Gets the stack by reference
array2 ret=pila.top();
return ret;
}
The questions are:
dmat sol
and I want to copy the stack pila to the first element in vector sol. Is this correct? sol[0]=lifo;
pila.top()[1]
legal?Upvotes: 0
Views: 84
Reputation: 254631
Is this correct? I mean, can I assign and return the element in my stack that way?
Now you've changed the type to match, yes. This will return a copy (not a reference) of the array on top of the stack.
In the original question, no; as I'm sure your compiler would have said. There's no implicit conversion between an array of short
(which top()
gives you) and an array of int
(which you try to assign that to). You can return the same array type by value or reference; or write a little conversion function if you need to change the type for some reason.
Is this correct?
sol[0]=lifo;
No; again, the compiler should have told you. You're trying to assign a stack
to an array
. Change the type of dmat
to vector<lifo>
, and you can copy a lifo
into it.
Is this syntax
pila.top()[1]
legal?
Yes, that gives the second element of the array on top of the stack. top()
returns a reference to that array, which [1]
can index.
Upvotes: 1
Reputation: 65720
No, this is not valid because the types of array2
and decltype(pila.top())
are disparate: array<short,2>
cannot be implicitly converted to an array<int,2>
. If you change the type of array2
then your program will be semantically correct.
No, sol[0] = lifo
is not valid as you are trying to assign a stack
to an array
Yes, pila.top()[1]
returns the second element of the array
at the top of the stack
.
Upvotes: 0