map containers

Apr 11, 2011 at 2:20pm
Hi

i am trying to write a programme which incorporates making a database using map containers. I have an issue with the fact that i am using a map to store the data from various derived classes.

I have an abstract base class and three derived classes from this abstract base class.

class A

class derived1:pubic A

class derived2:public A

class derived3: public A

I am trying to store information of the various types from each class in one map container, using an integer as my key. However, i cannot find a way of defining a map container so that it can change the type of each element as required to enable it to store all the data from each of the different classes.

I have used vector contaienrs to store data from derived classes before in conjunction with base class pointers, where i defined(using same class names as defined above):-

vector<*A> data;

data.push_back(new derived1(a, b, c ..etc));

this allowed me to store various types of data within a vector container.

I was hoping maybe someone could tell me a similiar method, or different (which ever works) to do the same with map containers?

Thanks

Michael
Apr 11, 2011 at 2:24pm
yes it's really similar to vector:

1
2
std::map<int, A*> m;
m[1] = new derived1(a, b, c ..etc);
Apr 11, 2011 at 3:02pm
Thank you so much, i was also wondering how i can insert the int into this map. is it possible to insert a pair into each element for example something like

m[1]= new pair<int, derived1>( int1, a etc);

i have tried writing this but it diddn't seem to work.

Or is there some other way i can insert the integer for each element into the map?

really appreciate the help.
Apr 11, 2011 at 3:15pm
It's ok now i figured it out, thank you for ur help.
Apr 11, 2011 at 5:20pm
map contains std::pair. Note that it sort it's content. The key is what's within the [] it could be any object that's sortable (overloaded operator<()).

You can also use vector if your key is just an integer. You just have to make the size of the vector large enough like
1
2
std:vector<A*> v(20); // Key 0 - 19;
v[1] = new derived1(a, b, c ..etc); // Key = 1 
Apr 12, 2011 at 2:59pm
If i was to use a mulitmap to do this what wouldbe the equivalent method for inserting data into the container.

So i would have

std::multimap<int, A*> m;

and something like this,

m[1] = new derived1(a, b, c ..etc);

I tried just doing this exactly but i got an error saying "no math for 'operator[]' in m[ID]
where ID is my integer
Apr 12, 2011 at 3:38pm
http://www.cplusplus.com/reference/stl/multimap/

According to the reference, there is no overloaded [] operator for multimaps like there is for maps.

http://www.cplusplus.com/reference/stl/multimap/insert/
Look at the example, it shows roughly what you might want and how to do it.
Apr 14, 2011 at 3:38pm
Thank you, i was also wondering if You may have any ideas for this question. I have posted it but unfortunately stil havent solved the issue,


Basically i have 3 classes derived from and abstract base class. In my main i have a switch case statement with three case's one for an object created of each class type. I have a function which is pretty much identical for in each of the cases in my main and i need to declare it within the classes using a pure virtual function in my abstract base class instead of simply writing it within the main crux of my code.

ostringstream ofss;

ofss <<" "<<clec<<" "<<cgrade<<" %"<<endl;

string outputFilename(ofss.str());

(clec and cgrade are previously declared)

This is few lines of code i have in my main of which i wish to write as a function inside my classes, and the pure virtual function of it declared in abstract base class, which will simply when called return a string.

A fellow cplusplus member did reply to me and suggest the following, of which i had already tried something similiar to but couldn't get it to work.

[" Try

virtual string void()=0;

and

string newcourse(string course, double grade)
{
ostringstream ofss;

ofss<<course<" "<<grade<<"%"<<endl;

string temp(ofss.str());
return temp;
}
in each of the 3 derived classes. "]

When i tried to implement this i jsut got a few errors within my classes, mentionin operands in reference to the "ofss<<course<" "<<grade<<"%"<<endl;" line.

Just in case it is important my classes are contained within a header file.

Apr 15, 2011 at 5:59am
Well, this line ofss<<course<" "<<grade<<"%"<<endl; must be ofss<<course<<" "<<grade<<"%"<<endl; // Note the second < after course

You can't write virtual string void()=0; since 'void' is a keyword and the meaning can't be changed. Provide another name instead.

If you want to make 'newcourse' overridable in your base class you must write virtual string newcourse(string course, double grade) = 0;.
Apr 15, 2011 at 2:21pm
Thanks for your help, got it working now.
Topic archived. No new replies allowed.