Hello,
Here is a part of my code:
char AARemove (char *alias) {
vector<AALIAS>::iterator it;
AALIAS aa;
if (alias != NULL) {
aa.Key = alias;
it = find (AAliases.begin (), AAliases.end (), aa);
printf ("Removing \"%s\" - \"%s\"\n", alias, it->Key.c_str ());
if (it != AAliases.end ()) {
// Remove it from the aliases list
printf ("Done: \"%s\"\n", AAliases.erase (it)->Key.c_str ());
return 2;
}
return 1;
}
return 0;
}
Where vector<AALIAS> AAliases;
The AALIAS class looks like this:
class AALIAS {
public:
string Key;
string Command;
bool operator<(const AALIAS &a) const;
bool operator==(const AALIAS &a) const;
AALIAS operator=(const AALIAS &a) const;
bool Set (char *cmd);
bool Add (char *cmd);
void Clear ();
};
Its operators:
bool AALIAS::operator<(const AALIAS &a) const {
return (Key < a.Key);
}
bool AALIAS::operator==(const AALIAS &a) const {
return (Key == a.Key);
}
AALIAS AALIAS::operator=(const AALIAS &a) const {
AALIAS aa;
aa.Key = a.Key;
aa.Command = a.Command;
return aa;
}
AAliases vector contains the following:
"cli" "cast lightning"
"ccl" "cast continual"
"ccf" "cast create"
"ccs" "cast create spring"
"cac" "cast acid"
"css" "cast stone"
"cl" "cast cure light"
"cb" "cast blindness"
"cgs" "cast giant"
"cs" "cast shield"
Where the first strings are the Keys and the second are Commands.
Now the problem is that when called with argument "cac",
the function AARemove prints out this:
Removing "cac" - "cac"
Done: "cac"
Which clearly indicates that the entry with Key "cac" wasn't removed.
Furthermore, for some reason it removed the last element of the AAliases vector.
However, when it->Key.c_str () is equal to "cac", then the iterator just can't be
pointing to the last element, can it?
It seems that I'm doing something wrong, as according to the documentation,
vector::erase shouldn't behave that way..
All suggestions are welcome,
Thank You
[Edited by - IndrekSnt on May 26, 2008 8:25:21 AM]