如何在map中按键删除元素,其中包含char和pair <int,string>

时间:2015-08-22 00:46:03

标签: c++ c++11 stl

我想删除所有首先配对的元素== 0

这里代码:

int main()
{
    map<char, pair<int,string>> myMap;
    map<char, pair<int,string>>::const_iterator it;
    for (int i = 0; i < 10; i++)
    {
        char c = 'a' + i;
        pair<int,string> p = make_pair(rand() % 2, "dd");
        myMap.insert(make_pair(c,p));
    }

    it = find_if(myMap.begin, myMap.end(), isEqual);

}

bool isEqual(const pair<char, pair<int, string> > element)
{
    return element.second.first == 0;
}

错误:

  

/ usr / include / c ++ / 4.8 / bits / stl_algo.h:150:错误:无法转换   &#39; __first.std::_Rb_tree_iterator<_Tp>::operator*<std::pair<const char, std::pair<int, std::basic_string<char> > > >()&#39;来自&#39; std::pair<const char, std::pair<int, std::basic_string<char> > >&#39;到&#39; std::pair<int, std::basic_string<char> >&#39;

   while (__first != __last && !bool(__pred(*__first)))

2 个答案:

答案 0 :(得分:1)

是的,当然。

我想我会编写类似这样的代码(我已经添加了一些额外的代码来打印删除前后的地图内容,以表明它有效):

#include <map>
#include <utility>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

ostream &operator<<(ostream &os, pair<int, string> const &p) {
    return os << "[" << p.first << ", " << p.second << "]";
}

int main() {
    map<char, pair<int, string>> myMap;
    for (int i = 0; i < 10; i++) {
        char c = 'a' + i;
        pair<int, string> p = make_pair(rand() % 2, "dd");
        myMap.insert(make_pair(c, p));
    }

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

    map<char, pair<int, string> >::iterator it;
    while (myMap.end() != (it = find_if(myMap.begin(), myMap.end(), [](auto p) { return p.second.first == 0; })))
        myMap.erase(it);

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

}

答案 1 :(得分:1)

我会用一个简单的循环来做到这一点。您需要记住在从地图中删除元素时跟踪迭代器:

#include <map>
#include <ctime>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>

int main()
{
    std::srand(std::time(0));

    std::map<char, std::pair<int, std::string>> myMap;

    for(int i = 0; i < 10; i++)
    {
        char c = 'a' + i;
        auto p = std::make_pair(rand() % 2, "dd");
        myMap.insert(std::make_pair(c, p));
    }

    std::cout << "\nbefore:\n";

    for(auto const& p: myMap)
    {
        std::cout << p.first;
        std::cout << " {" << p.second.first;
        std::cout << ", " << p.second.second;
        std::cout << "}" << '\n';
    }

    // remove items in a simple loop
    for(auto iter = myMap.begin(); iter != myMap.end();)
    {
        if(iter->second.first == 0)
            iter = myMap.erase(iter); // grab new iterator when erasing
        else
            ++iter; // otherwise just increment
    }

    std::cout << "\nafter:\n";

    for(auto const& p: myMap)
    {
        std::cout << p.first;
        std::cout << " {" << p.second.first;
        std::cout << ", " << p.second.second;
        std::cout << "}" << '\n';
    }

}

示例输出:

before:
a {0, dd}
b {1, dd}
c {0, dd}
d {0, dd}
e {1, dd}
f {0, dd}
g {0, dd}
h {1, dd}
i {0, dd}
j {0, dd}

after:
b {1, dd}
e {1, dd}
h {1, dd}