containers of arrays?

S

Stephen J. Fromm

I take it I cannot form a container of an array type? For example,
stack<string[3]> parentStack;

TIA,

sjfromm
 
J

John Dibling

I take it I cannot form a container of an array type? For example,
stack<string[3]> parentStack;

TIA,

sjfromm

You can have containers of arrays, but the declaration youv'e posted
above is faulty. Youv'e declared a stack where each element is 3
strings. This is fine in principle, but probaly what you should do is
declare a struct that defines the stack elements, then declare the
stack as a stack of those things, like this:

struct STRINGS
{
string m_Strings[3];
}; // STRINGS

int main()
{
std::stack<STRINGS> stk;
return 0;
}

</dib>
John Dibling
Witty banter omitted for your protection
 
R

Rolf Magnus

John said:
I take it I cannot form a container of an array type? For example,
stack<string[3]> parentStack;

TIA,

sjfromm

You can have containers of arrays,

No, you can't.
but the declaration youv'e posted
above is faulty. Youv'e declared a stack where each element is 3
strings. This is fine in principle,

No, it's not. Arrays are not assignable and as such, they cannot be used
as elements of standard containers. The above code should not be
accepted by your C++ compiler.
but probaly what you should do is
declare a struct that defines the stack elements, then declare the
stack as a stack of those things, like this:

struct STRINGS
{
string m_Strings[3];
}; // STRINGS

int main()
{
std::stack<STRINGS> stk;
return 0;
}

That would work.
 
S

Stephen J. Fromm

John Dibling said:
On 29 Jul 2003 10:18:57 -0700, (e-mail address removed) (Stephen J.
Fromm) wrote:

Thanks for your reply.
I take it I cannot form a container of an array type? For example,
stack<string[3]> parentStack;

TIA,

sjfromm

You can have containers of arrays, but the declaration youv'e posted
above is faulty. Youv'e declared a stack where each element is 3
strings. This is fine in principle, but probaly what you should do is
declare a struct that defines the stack elements, then declare the
stack as a stack of those things, like this:

Right, that's what I ended up doing.

What I'm curious about is whether I can *directly* define a stack<...>
of an array or not. My compiler decidedly didn't like it.

struct STRINGS
{
string m_Strings[3];
}; // STRINGS

int main()
{
std::stack<STRINGS> stk;
return 0;
}

</dib>
John Dibling
Witty banter omitted for your protection
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,800
Members
47,348
Latest member
Mikientp

Latest Threads

Top