Hello Everybody!
I am a newbie in c++. I want to create a simple application that insert, search and displays data. And I am planning to use an STL Container.
What kind of STL Container am I going to use that stores 3 different data types?
example: int, bool, double
Thank you
10 2921
There are no Standard containers that allow multiple types. Try Boost or try using a variant or as a last resort a union
or you could just use class with the three types: -
class myClass
-
{
-
public:
-
int myInt;
-
bool myBool;
-
double myDouble;
-
};
-
and then use this class with a vector or the like: -
std::vector<myClass> myVector;
-
or store pointers in the vector -
std::vector<myClass*> myVector;
-
I want to create a simple application that insert, search and displays data. And I am planning to use an STL Container.
If this made sense, the STL would have a container for it. However, with multiple types (like bool, int and double) you cannot do inserts as there is no way to compare a bool to a double or a double to an int. Comparison is also involved in doing a search.
You might not even be able to display if you allow objects of multiple classes.
I think your data model is incorrect.
What is it you are trying to do??
Thanks for the replies.
I have use the STL Container Map that stores 2 values and it works.
Below you'll find my code:
class ContainerValues
{
public: ContainerValues(void);
~ContainerValues(void);
void Update(long dwHandle, double dValue);
bool Remove(long Handle);
double Sum();
void Display(); private: map<long, double>m_Map;
typedef map<long, double>::iterator IT; };
void ContainerValues::Update(long dwHandle, double dValue)
{ Remove(dwHandle);
m_Map.insert(pair<long, double>(dwHandle, dValue)); } // end ContainerValues::Update
But I need to make it 3 values.
like: map<long, double,double>m_Map;
Then instead of pair
m_Map.insert(pair<long, double>(dwHandle, dValue));
what am I going to use since I need to make it 3 values?
Thanks
map<long, double>m_Map;
This does not store two values.
It stores one value. A pair.
In a map, the pair is a key/value object. That is, you find the double (a value) by asking for the long (a key).
My Post #5 still holds.
Ok here is a union solution -
#include <iostream>
-
#include <vector>
-
-
class multitype
-
{
-
friend std::ostream& operator<<(std::ostream& os, const multitype& m);
-
-
public:
-
multitype()
-
{
-
value.i = 0;
-
type = INT;
-
};
-
multitype(bool in)
-
{
-
value.b = in;
-
type = BOOL;
-
}
-
multitype(int in)
-
{
-
value.i = in;
-
type = INT;
-
}
-
multitype(double in)
-
{
-
value.d = in;
-
type = DOUBLE;
-
}
-
-
enum TYPE {BOOL, INT, DOUBLE};
-
union VALUE
-
{
-
bool b;
-
int i;
-
double d;
-
};
-
-
-
private:
-
-
VALUE value;
-
TYPE type;
-
};
-
-
std::ostream& operator << (std::ostream& os, const multitype& m)
-
{
-
switch(m.type)
-
{
-
case multitype::BOOL:
-
return os << m.value.b;
-
//return os << (m.value.b ? "true" : "false");
-
case multitype::INT:
-
return os << m.value.i;
-
case multitype::DOUBLE:
-
return os << m.value.d;
-
}
-
}
-
-
-
int main()
-
{
-
std::vector<multitype> foo;
-
-
foo.push_back(20);
-
foo.push_back(true);
-
foo.push_back(3.14);
-
-
std::cout << foo[0] << "\n" << foo[1] << "\n" << foo[2] << "\n";
-
-
}
-
Missing:
Test what type is being held
Assignment to the various types
enum TYPE {BOOL, INT, DOUBLE};
union VALUE
{
bool b;
int i;
double d;
};
private:
VALUE value;
TYPE type;
You have just recoded the Microsoft VARIANT. The code for this on an MSDN web page so just copy if off.
Again, I say, you cannot do much with containers of multi-types, like VARIANT, since the elements are incompatible with each other. You can't compare elements so you can't sort them or use a tree to access them by key, or sort, or do any ordering or assignment whatsoever.
The VARIANT was invented for Visual Basic to communicate with C functions and not for containers of VARIANT.
Discriminated unions like this are a C solution for templates.
The OP has not given the reason for needing to store 3 types in one element of a container. I expect 3 containers are what's needed.
I could think of a use, like a table or spreadsheet where different cells need different types of values yet a central storage.
btw, The MS VARIANT is probably more encompassing than my implementation, I have no provision for any kind of a string.
I have already tried the #4 and it is now working.
Thanks to everyone for all the help.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Nafai |
last post by:
Hi I want to write a function which erases al the repeated elements in a
range. How should be the prototype?
template <class Iterator>
void eraseRepeated(Iterator begin, Iterator end);
...
|
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
|
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...
|
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...
|
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,...
|
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.
...
|
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...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
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...
| |