How to use map containers whith odjects of user classes

  • Thread starter Hatzigiannakis Nikos
  • Start date
H

Hatzigiannakis Nikos

I want to use a map container with objects of the following class
rectangle. Each object will be accosiated with a key of type string:.

#include <iostream>
#include <string>
#include <map>

using namespace std;

class rectangle
{
float side_a;
float side_b;
public:
float area() {return side_a * side_b;}
rectangle(float a, float b) {side_a=a; side_b=b;}
};

map<string,rectangle> mymap;

main()
{
rectangle rec1(10,20);

//the following statment does not compile
mymap["table"] = rec1;
system("pause");
}

1. The statment mymap["trapezi"] = rec1; supposed to add a new element in
the map with the key "table" and value the object rec1 but it doesnt
compile. why?

2. If I finaly manage to add a new element how can I have access to the
mebers of the rectangle object in a map element ?

Thanks a lot
 
K

Kai-Uwe Bux

Hatzigiannakis said:
I want to use a map container with objects of the following class
rectangle. Each object will be accosiated with a key of type string:.

#include <iostream>
#include <string>
#include <map>

using namespace std;

class rectangle
{
float side_a;
float side_b;
public:
float area() {return side_a * side_b;}
rectangle(float a, float b) {side_a=a; side_b=b;}
};

map<string,rectangle> mymap;

main()
{
rectangle rec1(10,20);

//the following statment does not compile
mymap["table"] = rec1;
system("pause");
}

1. The statment mymap["trapezi"] = rec1; supposed to add a new element in
the map with the key "table" and value the object rec1 but it doesnt
compile. why?

The class rectangle has no default constructor.

2. If I finaly manage to add a new element how can I have access to the
mebers of the rectangle object in a map element ?

You could do:

rectangle & rect = mymap["table"];
// now use rect.


Best

Kai-Uwe Bux
 
T

thomas

map<string,rectangle> mymap;

main()
{
    rectangle rec1(10,20);

    //the following statment does not compile
    mymap["table"] = rec1;
    system("pause");

}
mymap["table"] cannot be a left value.
mybe you need to redefine mymap:

map<string,rectangle&> mymap;
 
K

Kai-Uwe Bux

thomas said:
map<string,rectangle> mymap;

main()
{
rectangle rec1(10,20);

//the following statment does not compile
mymap["table"] = rec1;
system("pause");

}
mymap["table"] cannot be a left value.

What makes you think that? The return type of non-const operator[] in a
std::map is mapped_type&. The operator[] is overloaded precisely so that
you can assign into the map. However, it required the mapped_type to be
default constructible.

mybe you need to redefine mymap:

map<string,rectangle&> mymap;

You did not run that by a compiler, did you? If you did and if the compiler
did not choke, may I suggest you ditch that compiler.


Best

Kai-Uwe Bux
 
J

James Kanze

I want to use a map container with objects of the following
class rectangle. Each object will be accosiated with a key of
type string:.
#include <iostream>
#include <string>
#include <map>
using namespace std;
class rectangle
{
float side_a;
float side_b;
public:
float area() {return side_a * side_b;}
rectangle(float a, float b) {side_a=a; side_b=b;}
};
map<string,rectangle> mymap;
main()
{
rectangle rec1(10,20);
//the following statment does not compile
mymap["table"] = rec1;
system("pause");
}
1. The statment mymap["trapezi"] = rec1; supposed to add a new element in
the map with the key "table" and value the object rec1 but it doesnt
compile. why?

The sub-expression mymap["trapezi"] returns a reference to the
object. If the object doesn't exist, it creates it, using the
object type's default constructor. Since rectangle doesn't have
a default constructor, it fails. (Note that any use of [] will
fail, even if not being used for insertion.)

The "standard" way to insert an object into a map is with
map<>::insert. The "standard" way to read an object present in
the map is with map<>::find. The [] in map is a convenience
function, for certain specific uses of map, and certain uses
only. (There is no one semantic that it could be given which
would be appropriate for all uses.)

You can either provide rectangle with a default constructor
(probably by specifying default arguments to the present
constructor), or use the "standard" functions insert and find to
access the map. Which solution is best depends on your
application.
2. If I finaly manage to add a new element how can I have
access to the mebers of the rectangle object in a map element?

See above. Either find(), or operator[]. Just be aware that
operator[] will insert an entry if one is not already present,
which may not be what you want.
 
H

Hatzigiannakis Nikos

Ok I put some default values in the constructor and worked fine.

I tried to use the insert method

mymap,insert("new",rectangle(12,12))

but it didnt work. Do I have any mistake?
 
K

kasthurirangan.balaji

Ok I put some default values in the constructor and worked fine.

I tried to use the insert method

mymap,insert("new",rectangle(12,12))

but it didnt work. Do I have any mistake?

map stores values as pair. you can use std::pair or std::make_pair

mymap.insert(std::pair<std::string,rectangle>("new",rectangle(12,12)));

Thanks,
Balaji.
 
J

James Kanze

Ok I put some default values in the constructor and worked fine.
I tried to use the insert method

but it didnt work. Do I have any mistake?

Yes. You didn't read the documentation first. (I like to be
helpful, but not even getting the number of arguments right
can't be a problem of understanding.)
 
M

Martin York

map stores values as pair. you can use std::pair or std::make_pair

mymap.insert(std::pair<std::string,rectangle>("new",rectangle(12,12)));

Thanks,
Balaji.

Though this is perfectly valid. Personally, I prefer to use
make_pair() as this automatically gets the correct types from the
parameters we use.

mymap.insert(std::make_pair(std::string("new"),rectangle(12,12)));
 
J

James Kanze

Though this is perfectly valid. Personally, I prefer to use
make_pair() as this automatically gets the correct types from the
parameters we use.
mymap.insert(std::make_pair(std::string("new"),rectangle(12,12)));

Actually, it doesn't. It does get a pair which will convert to
the correct type, if the arguments are acceptable types, and if
the library is up to date, but the correct type is
MapType::value_type. If you have a typedef for the map:

mymap.insert( Map::value_type( "new", rectangle(12,12) ) ) ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top