samedi 9 octobre 2021

Why std::map::erase returns int rather than bool?

I wonder why std::map::erase has an overload that returns an int that represents the number of elements erased; so as long as the elements are unique so the number is either 1 or 0. In this case why it doesn't return bool rather than an int?

     std::map<std::string, std::size_t> containers{
    {"map", 1}, {"set", 10}, {"map", 5}, {"vector", 4}, {"array", 7}
};

for(auto const& p : containers)
    std::cout << p.first << " " << p.second << '\n';

std::cout << containers.erase("map") << '\n'; // 1
std::cout << containers.erase("map") << '\n'; // 0

for(auto const& p : containers)
    std::cout << p.first << " " << p.second << '\n';

Aucun commentaire:

Enregistrer un commentaire