473,769 Members | 6,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4096
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<fooB ase*> 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.goo gle.com...
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.goo gle.com...
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.goo gle.com...
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.goo gle.com...
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<boos t::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<floa t>(data[i]))
do_smth_on_floa t(*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***********@y ahoo.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<type s> d;
d.push_back(aFo oFloat);
d.push_back(aFo oInt);

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_pt r<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
2788
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 |bo|int | char |etc.
9
3734
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 depends on the size of its members even if the deque is empty? is there at all a way to check out how much memory my deque occupies? i've read that the sizeof operator cannot be used with dynamically allocated arrays so i figured it wouldn't give...
8
1621
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 index variable have? size_t? vector<X>::size_type? vector<Y>::size_type? Example code: -------------------- vector<X> xv; vector<Y> yv;
5
3504
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() self.rotate(pos) return ret
2
1673
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 data). The problem that I try to solve is the following. Imagine a class "Attribute" that needs to save multiple "Parameters" (the number of parameters for 1 attribute will vary each time we run the application). Each parameter can be of a different...
20
1959
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. Feedback/suggestions/criticism is welcome. ''' 2006.12.21 Created. ''' import unittest
9
1567
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 (3) often in ppl' code, does that give any benefit over (2) Thanks for answering in advance:) -------------------------------
15
3535
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 you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
4
5860
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:
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10215
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.