472,989 Members | 3,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

how to store different types of data in deque?

Hi,

I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.

template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};

After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?
Jul 22 '05 #1
7 3997
Jenny wrote:

Hi,

I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.

template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};

After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?


You need polymorphic behaviour, thus
* Derive foo from a common base classs
* store pointers in the deque instead of objects.

class fooBase
{
protected:
std::string name;
};

template <class TYPE>
class foo : public fooBase
{
protected:
std::deque<TYPE> data;
};

std::deque<fooBase*> foo_list;

Attention: Since you are storing pointers, you are now responsible
for deleting the foo objects. The deque won't do it any longer.
You can get around this by using a smart pointer you can get
eg. at www.boost.org

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

"Jenny" <je***********@yahoo.com.au> wrote in message
news:69**************************@posting.google.c om...
Hi,

I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.

template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};

After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?


Polymorphism (as recommended by Karl) is the usual way to do this. But in
this case you have another choice, store your ints and floats as strings.

class foo
{
protected:
std::string name;
std::deque<std::string> data;
};

It's easy enough to convert from strings to ints and floats and back. Easy
enough to tell if a string is an int or a float. Just a suggestion and a bit
of a hack, but maybe it would be a good idea.

john
Jul 22 '05 #3
"Jenny" <je***********@yahoo.com.au> wrote in message
news:69**************************@posting.google.c om...
I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque. .... After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?

Previous posters have suggested:
- storing a textual representation into std::string-s
- a container of polymorphic elements (storing a ptr to a base class).
Another option would be to use a C-style union (usually in conjunction
with a typeId field), but let's not insist on this.

However, the problem you are facing is common enough, and encapsulated
solutions already exist: classes that can store a value of one of
several types. Some vendors (MS) call this a Variant, other call
them *discriminated unions* (because they are like C-style unions,
but provide information about the stored type).
http://www.google.com/search?q=discr...+union+C%2B%2B

The boost library provides such a class, called "boost::any".
You may want to give it a try:
http://www.boost.org/doc/html/any.html
In particular, take a look at the example:
http://www.boost.org/doc/html/ch02s02.html
Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #4

"John Harrison" <jo*************@hotmail.com> wrote in message news:2s*************@uni-berlin.de...

"Jenny" <je***********@yahoo.com.au> wrote in message
news:69**************************@posting.google.c om...
Hi,

I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.

template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};

After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?


Polymorphism (as recommended by Karl) is the usual way to do this. But in
this case you have another choice, store your ints and floats as strings.

class foo
{
protected:
std::string name;
std::deque<std::string> data;
};

It's easy enough to convert from strings to ints and floats and back. Easy
enough to tell if a string is an int or a float. Just a suggestion and a bit
of a hack, but maybe it would be a good idea.

john


It would be a good idea not only for ints and floats, but for other types if are using stream_cast.

stream_cast() was discussed by Dietmar Kuehl at
* http://groups.google.com/groups?selm...nnrp1.deja.com

To be sure that stream_cast conversion is bijective, one could use functions which detect stream_cast bijectivity
* http://groups.google.com/groups?th=8bb967e46c5e3a87
* http://groups.google.com/groups?thre...0uni-berlin.de
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #5
"John Harrison" <jo*************@hotmail.com> writes:
"Jenny" <je***********@yahoo.com.au> wrote in message
news:69**************************@posting.google.c om...
Hi,

I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.

template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};

After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?


Polymorphism (as recommended by Karl) is the usual way to do this. But in
this case you have another choice, store your ints and floats as strings.

class foo
{
protected:
std::string name;
std::deque<std::string> data;
};

It's easy enough to convert from strings to ints and floats and back. Easy
enough to tell if a string is an int or a float. Just a suggestion and a bit
of a hack, but maybe it would be a good idea.

Alternatively you can use boost::any:
class foo
{
protected:
std::string name;
std::deque<boost::any> data;
};

And then you check the type of the object stored in runtime
data[i].type() == typeid();
int x = boost::any_cast<int>(data[i]);

Or you can use boost::variant:
class foo
{
protected:
std::string name;
std::deque< boost::variant<int, float> > data;
};

And then:
if (int* x = boost::get<int>(data[i]))
do_smth_on_int(*x);
else if (float* x = boost::get<float>(data[i]))
do_smth_on_float(*x);

--
WBR, Max Vasin
JID: ma******@jabber.ru
ICQ: 276438891

Jul 22 '05 #6
> Alternatively you can use boost::any:
Or you can use boost::variant:


Doh! I never heared of these two. And wow, boost::any looks really
interesting...
-Gernot
Jul 22 '05 #7
On 4 Oct 2004 05:13:47 -0700, je***********@yahoo.com.au (Jenny)
wrote:
Hi,

I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.

template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};

After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?


boost::variant lets you do this:

typedef boost::variant<foo<int>, foo<float> > types;
std::deque<types> d;
d.push_back(aFooFloat);
d.push_back(aFooInt);

Then apply_visitor is the best way to get at the values, but you may
also use get:
foo<float>& foo = get<foo<float> >(d[0]);

See http://www.boost.org/doc/html/variant.html.

In your case, a better approach might be to give foo a non-templated
base class, and use a deque<shared_ptr<foo_base> >.

Tom
Jul 22 '05 #8

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

Similar topics

4
by: m | last post by:
Hello I need to use sth. like container for different types of variables. Is sth like this in c++ STL? ----|----------|--|----|------|etc. var1| var2 |v3|var4| var5 |etc. int | float...
9
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque...
8
by: ALiX | last post by:
Hi all, In my code I use different vectors of the same size to hold some data. At some point I need to iterate through all vectors at the same time. Now, the question is what type should the...
5
by: Russell Warren | last post by:
Does anyone have an easier/faster/better way of popping from the middle of a deque than this? class mydeque(deque): def popmiddle(self, pos): self.rotate(-pos) ret = self.popleft()...
2
by: mast2as | last post by:
Hi there, for a long time I've been trying to think of way of saving different data of different types using one single class (well 2 in reality, a class for the data, and 1 class for a list of...
20
by: Chris | last post by:
I'm not sure if this has been done before, but I couldn't easily find any prior work on Google, so here I present a simple decorator for documenting and verifying the type of function arguments....
9
by: newbie | last post by:
Let's see two different usage of an STL container. I see (2) more often when reading code over (1), dose that give any benefit or it's purely a coding preference? Also, please see the (3), I see...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
4
by: Edward Jensen | last post by:
Hi, I have the following static arrays of different size in a class: in header: static double w2, x2; static double w3, x3; static double w4, x4; in GaussLegendre.cpp:
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.