Atiqur Rahman
unread,Feb 6, 2009, 9:51:13 AM2/6/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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.