Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erase specific elements in std::map

I want to erase some elements in my std::map.
I wrote erase + remove_if technique which I always do with other sequence containers.
But it wasn't compile with map. Why?
And How can I do this job?

std::map<int, int> m;

bool foo(const std::pair<int, int>& p)
{
    return p.second > 15;
}

int _tmain(int argc, _TCHAR* argv[])
{
    m.insert(make_pair(0, 0));
    m.insert(make_pair(1, 10));
    m.insert(make_pair(2, 20));
    m.insert(make_pair(3, 30));

    m.erase(
        remove_if(m.begin(), m.end(), foo),
        m.end()); // compile error

    return 0;
}
like image 752
Benjamin Avatar asked Aug 10 '11 08:08

Benjamin


People also ask

How do I delete a key-value pair in maps?

Using erase() : erase() is used to erase the pair in map mentioned in argument, either its position, its value or a range of number. erase(key) : Erases the key-value pair using key mentioned in its argument.

How do you clear data on a map in C++?

map::clear() function is an inbuilt function in C++ STL, which is defined in header file. clear() is used to remove all the content from the associated map container. This function removes all the values and makes the size of the container as 0.

How do I remove all elements from a map?

clear() The clear() method removes all elements from a Map object.


1 Answers

Write it like this for map, since remove_if won't work for map iterators (it merely puts offending elements at the end, and map iterators don't allow for this):

template <typename Map, typename F>
void map_erase_if(Map& m, F pred)
{
    typename Map::iterator i = m.begin();
    while ((i = std::find_if(i, m.end(), pred)) != m.end())
        m.erase(i++);
}

or if you like one-liners:

template <typename Map, typename F>
void map_erase_if(Map& m, F pred)
{
    for (typename Map::iterator i = m.begin();
         (i = std::find_if(i, m.end(), pred)) != m.end();
         m.erase(i++));
}
like image 150
Alexandre C. Avatar answered Oct 04 '22 14:10

Alexandre C.