473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

STL Container

7
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
Aug 3 '07 #1
10 2921
afraze
18
lourk,

you should look over url below.

http://www.cplusplus.com/reference/stl/

After that if you have any question, we can help u..
Aug 3 '07 #2
Darryl
86
There are no Standard containers that allow multiple types. Try Boost or try using a variant or as a last resort a union
Aug 3 '07 #3
JonLT
41
or you could just use class with the three types:
Expand|Select|Wrap|Line Numbers
  1. class myClass
  2. {
  3.    public:
  4.      int myInt;
  5.      bool myBool;
  6.      double myDouble;
  7. };
  8.  
and then use this class with a vector or the like:
Expand|Select|Wrap|Line Numbers
  1. std::vector<myClass> myVector;
  2.  
or store pointers in the vector
Expand|Select|Wrap|Line Numbers
  1. std::vector<myClass*> myVector;
  2.  
Aug 3 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
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??
Aug 3 '07 #5
lourk
7
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
Aug 6 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 6 '07 #7
Darryl
86
Ok here is a union solution

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class multitype
  5. {
  6.     friend std::ostream& operator<<(std::ostream& os, const multitype& m);
  7.  
  8. public:
  9.     multitype()
  10.     {
  11.         value.i = 0;
  12.         type = INT;
  13.     };
  14.     multitype(bool in)
  15.     {
  16.         value.b = in;
  17.         type = BOOL;
  18.     }
  19.     multitype(int in)
  20.     {
  21.         value.i = in;
  22.         type = INT;
  23.     }
  24.     multitype(double in)
  25.     {
  26.         value.d = in;    
  27.         type = DOUBLE;
  28.     }
  29.  
  30.     enum TYPE {BOOL, INT, DOUBLE};
  31.     union VALUE
  32.     {
  33.         bool        b;
  34.         int        i;
  35.         double    d;
  36.     };
  37.  
  38.  
  39. private:    
  40.  
  41.     VALUE value;
  42.     TYPE type;
  43. };
  44.  
  45. std::ostream& operator << (std::ostream& os, const multitype& m)
  46. {
  47.     switch(m.type)
  48.     {
  49.     case multitype::BOOL:
  50.         return os << m.value.b;
  51.         //return os << (m.value.b ? "true" : "false");
  52.     case multitype::INT:
  53.         return os << m.value.i;
  54.     case multitype::DOUBLE:
  55.         return os << m.value.d;
  56.     }
  57. }
  58.  
  59.  
  60. int main()
  61. {
  62.     std::vector<multitype> foo;
  63.  
  64.     foo.push_back(20);
  65.     foo.push_back(true);
  66.     foo.push_back(3.14);
  67.  
  68.     std::cout << foo[0] << "\n" << foo[1] << "\n" << foo[2] << "\n";
  69.  
  70. }
  71.  
Missing:

Test what type is being held
Assignment to the various types
Aug 6 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 6 '07 #9
Darryl
86
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.
Aug 6 '07 #10
lourk
7
I have already tried the #4 and it is now working.

Thanks to everyone for all the help.
Aug 7 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
19
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); ...
3
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
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...
7
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...
11
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,...
2
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. ...
1
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...
18
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
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
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...
0
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,...
0
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...
0
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...

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.