STL map.

2 views
Skip to first unread message

Atiqur Rahman

unread,
Feb 6, 2009, 9:51:13 AM2/6/09
to Online Judge Helps
Using map is much easier.
Declaration is like this:

map<type, type> varName;
map<type, type>::iterator p;

To reset contents of a map,
varName.clear()

To insert a pair into map,
varName.insert(pair<type, type>(typeValue, typeValue));

Find an element (search using the first key of pair)
p = varName.find(value);
a = p->second;

Now value of a is the second key of pair.

Actually, here my topic is to how will you map a string to integer.
This way I did it. If you have a better idea you can post.

Declare a class with that string

class vehicle {
char str[40];
public:
vehicle() { }
vehicle(char *s) { strcpy(str,s); }
char *get() { return str; }
void show() { puts(str); }
};

Overload the '<' operator:

bool operator<(vehicle a, vehicle b) {
return (strcmp(a.get(), b.get())<0);
}

Decalaration:
map<vehicle, int> cargo;
// if you are need of an iterator
map<vehicle, int>::iterator p;

Insertion
scanf("%s", str);
cargo.insert(pair<vehicle, int>(vehicle(str), ind++));

Find works the same way.

MAK kader

unread,
Feb 6, 2009, 11:57:36 AM2/6/09
to online-ju...@googlegroups.com
thats good ,
how to use pair?  That's my next question.



MAK
Fond Of All New
Home: www.mak11.co.cc





> Date: Fri, 6 Feb 2009 06:51:13 -0800
> Subject: STL map.
> From: Uni...@gmail.com
> To: online-ju...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages