I am trying to find a skill in a list of strings named Skills. I want to be able to remove said skill like this:
1 2 3 4 5 6 7 8
void Player::removeskill(const std::string skill)
{
for(std::list<std::string>::iterator sit = Skills.begin(); sit !=Skills.end(); ++sit)
{
if(/*if the iteraor position has a skill named "std::string skill"*/)
Skills.erase(*sit);
}
}
The line that has if(/*if the iteraor position has a skill named "std::string */ is the one I am having trouble finding. I don't know how to test the data at the iterator's position in a function. Am I even doing this right in the first place?
Just change your list for a set.
In addskill(), use insert() instead of push_back(). Skills will be sorted alpahabetically in the set at insertion, and will prevent multiple insertion of the same skill
Then use find() on the set for a given skill nalme (eventually with a custom comparison function if you want to find skill name with differnet upper/lower case characters)
bartoli,
Are you referring to the boost library or the STL library? I've never used boost but I'm told it has a lot of very good stuff in it. I don't recall hearing much about set's. So I am unsure which library contains a set container.
iHutch105,
Thanks for your help. The vector::at() seems promising. You say vector is better for random access?
The question arises do you want to remove all elemennts in the list that are equal to a given sttring or only the first one?
To remove all elements equal to a given string you can use the combination of standard algorithm std::remove and list member function erase. For example