2016-12-16 56 views

回答

6

下面的代码应该工作:

std::multimap<int, int> M; 
// initialize M here 
auto it = M.upper_bound(20); 
M.erase(M.begin(), it); 

只需使用upper_bound然后erase

0
#include <map> 
#include <utility> 
#include <iostream> 
#include <iterator> 
#include <algorithm> 
using namespace std; 

int main() 
{ 
    multimap<int, int> mp; 

    mp.insert(make_pair(10, 20)); 
    mp.insert(make_pair(11, 22)); 
    mp.insert(make_pair(12, 24)); 
    mp.insert(make_pair(12, 25)); 
    mp.insert(make_pair(13, 26)); 
    mp.insert(make_pair(24, 27)); 
    mp.insert(make_pair(25, 29)); 
    mp.insert(make_pair(26, 30)); 

    for(auto& elem : mp) 
     cout<<" first = "<<elem.first<<" second = "<<elem.second<<endl; 
    cout<<endl; 

    for(auto pos = mp.begin(); pos!=mp.end();) 
    { 
     if(pos->first <= 20) 
      mp.erase(pos++); 
     else 
      ++pos; 
    } 

    for(auto& elem : mp) 
     cout<<" first = "<<elem.first<<" second = "<<elem.second<<endl; 

    return 0; 
}