I'm doing this all in classes, I've a simple question, I've a private list of pairs:
std::list<std::pair<std::string, size_t>> pokemons_;
to which I've passed certain values as:
{("Pikachu", 25),
("Raticate", 20),
("Raticate", 20),
("Bulbasaur", 1),
("Pikachu", 25),
("Diglett", 50)};
Now I want to remove a pair by calling a public remove function of my class.
bool PokemonCollection::Remove(const std::string& name, size_t id){};
what I don't understand is how to compare the string and id value while calling the remove function:
collection.remove("Raticate", 20);
"collection Is an object of my class"
what I've implemented till now by the help of other forums and reading internet and cpp reference is:
bool PokemonCollection::Remove(const std::string& name, size_t id){
bool found;
string x;
size_t y;
for (auto currentPair : pokemons_){
pair<string, size_t> currentpair = currentPair;
x=currentpair.first;
y=currentpair.second;
pokemons_.erase(pokemons_.begin() i)
for (int i=0; i<pokemons_.size(); i ){
if (pokemons_[i].first == x && pokemons_[i].second == y){
// pokemons_.erase(pokemons_.begin() i);
cout<<"FOUND!!!!!!!!!!!!!!!";
found = true;
return true;
}else{
found = false;
}
}
}
return found;
}
but this remove function of mine gives some errors I don't really understand. also it gives so much errors on the commented line where I used erase function. I just need some help to compare the string and id and remove that pair from original private list of my class.
MY FUNCTION
```bool PokemonCollection::Remove(const std::string& name, size_t id){
bool found;
//string x;
//size_t y;
pokemons_.remove_if([&](std::pair<std::string, size_t>& p){return found = true and p.first==name and p.second==id;});
if(found==true){
return true;
}else{
found = false;
}
return found;
}```
CodePudding user response:
There is a very simple solution to your problem.
The std::list
has a function remove_if
, which will do everything for you. See here.
Please see the below code as an example:
#include <iostream>
#include <string>
#include <utility>
#include <list>
std::list<std::pair<std::string, size_t>> poke
{{"Pikachu", 25},
{"Raticate", 20},
{"Raticate", 20},
{"Bulbasaur", 1},
{"Pikachu", 25},
{"Diglett", 50}};
void remove(const std::string& s, size_t i) {
poke.remove_if([&](std::pair<std::string, size_t>& p){return p.first== s and p.second==i;});
}
int main() {
remove("Raticate", 20);
for (const auto& [s,i] : poke)
std::cout << s << '\t' << i <<'\n';
}
CodePudding user response:
It can be done even simpler than Armin Montigny's solution. std::pair<>
comes with comparison operators, so you don't need a custom function to check if an element of the list is equal to a given pair:
void remove(const std::string& s, size_t i) {
poke.remove({s, i});
}
Since C 20, std::list
's remove()
and remove_if()
return a count of the number of elements removed. If you need to be compatible with earlier versions, you could always check the result of poke.size()
before and after the call to remove()
to see if the size of the list has changed.