By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,191 Members | 2,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,191 IT Pros & Developers. It's quick & easy.

how to store different types of data in deque?

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a

"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

P: n/a
"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

P: n/a

"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

P: n/a
"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

P: n/a
> 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.