STL complient container?

J

Jon Slaughter

I made a simple container that contains an STL container(basicaly a wrapper)
and I want to use for each on it just like I would with an STL container.
How do I make my container STL complient so I can do something like the
following:


class Container
{
private:
vector<int> V;
public:
.........
};


void main()
{

for each (int i in Container)
{
*******************
};

return;
};


Any ideas? I'm looking for a very simple(less code) method to access the
elements of my container without using an iterator and this is as close as I
have seen since its works very very nice for STL containers... now if I
could easily make it work for mine.

Thanks,

Jon
 
R

Rui Maciel

Jon said:
I made a simple container that contains an STL container(basicaly a
wrapper) and I want to use for each on it just like I would with an STL
container. How do I make my container STL complient so I can do something
like the following:


class Container
{
private:
vector<int> V;
public:
........
};

Overload operator [] and make it return v

void main()

No no no.... the main function MUST return an int.
int main()
{ Container c;
for each (int i in Container)
{
*******************
std:: cout << c << std::endl; // there you go
};

return 0;
};

I believe that iterators are by far the best way to iterate through the
elements but I believe that this solution also works.


Hope this helps
Rui Maciel
 
M

Marc Mutz

Jon Slaughter wrote:
I made a simple container that contains an STL
container(basicaly a wrapper) and I want to use for each
on it just like I would with an STL container. How do I
make my container STL complient so I can do something
like the following:
<snip>

Have a look at the implementation of std::queue and
std::stack, which are container adapters.

Marc
 

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

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top