473,563 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL complient container?


I made a simple container that contains an STL container(basic aly 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
Aug 28 '05 #1
3 1146
Jon Slaughter wrote:

I made a simple container that contains an STL container(basic aly 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[i]

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[i] << 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
--
Running Kubuntu 5.04 with KDE 3.4.2 and proud of it.
jabber:ru****** **@jabber.org
Aug 28 '05 #2
Hi,

You can do either of two things. First implement the interface the for_each
operator expects:

First:
http://www.sgi.com/tech/stl/table_of_contents.html

(Look at for_each and the iterator)

Second (the lazy approach):

Just make variables and functions in your class that actually delegate
things to the ones in the vector

e.g.

begin() { return Vector.begin(); }
end() { return Vector.end(); }

etc.


--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Jon Slaughter" <Jo***********@ Hotmail.com> wrote in message
news:11******** *****@corp.supe rnews.com...

I made a simple container that contains an STL container(basic aly 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

Aug 28 '05 #3
Jon Slaughter wrote:
<snip>
I made a simple container that contains an STL
container(basic aly 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
Aug 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1547
by: Maitre Bart | last post by:
What I want to perform is calling a member function of container 1 (CRunner), using as argument the value of a container 2 (CNames). The purpose is to transfer values from C2 into C1 using a specific C1 member function. Both containers contains the same number of elements (or at least, C1 is larger than C2). As suggested by S. Meyers (and...
3
2620
by: jignesh shah | last post by:
Hi all, Is there a way to recover a single container if its been corrupted or mark bad without restoring whole tablespace? environment: db28.1/aix5.1/tsm/rs-6000. Regards Jignesh
9
1781
by: horizon5 | last post by:
Hi, my collegues and I recently held a coding style review. All of the code we produced is used in house on a commerical project. One of the minor issues I raised was the common idiom of specifing: <pre> if len(x) 0: do_something() </pre>
7
20001
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low cost. STL vector is suitable for removing form tail? or it is as costly as removing from middle? any other std container to serve this purpose?...
11
1668
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However, the behaviours of std::vector::iterator can not meet my requirements. I need to redefine the vector::iterator's behavious such as ++. It means...
2
1977
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. class AnyObject: pass class Container: links = def add(self,other):
1
5910
by: Miked | last post by:
Hello: I'm relatively new to CSS, and I'm doing a site where I don't want to use any tables. I've gotten pretty far, and the site has the layout I want. My only problem is that I'm using the flot positional paremter a lot on containers, and it really seems to do what I want. Except when I put other containers in these containers, they...
18
1801
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
36
2010
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are there any other no-overhead ways that a contained class can access its container? The obvious choice of passing (a pointer or a reference to the...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.