multimap容器如何删除某键下指定的值?

Herro 2005-07-31 08:37:20
multimap<string, string> a;
typedef pair<string, string> Pair;

a.insert( Pair("name", "value1") );
a.insert( Pair("name", "value2") );
a.insert( Pair("name", "value3") );

我要删除"name", "value2"的话如何弄?

谢谢。。。
...全文
718 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lw1a2 2005-07-31
  • 打赏
  • 举报
回复
我的错了,重新写一个:

#include <iostream>
#include <map>
#include <utility>
#include <string>
using namespace std;

multimap<string, string> a;
typedef pair<string, string> Pair;



int main()
{
a.insert( Pair("name", "value1") );
a.insert( Pair("name", "value2") );
a.insert( Pair("name", "value3") );

pair<multimap<string, string>::iterator,multimap<string, string>::iterator> pos;
pos=a.equal_range("name");
multimap<string, string>::iterator it;
for(it=pos.first;it!=pos.second;++it)
if(it->second=="value2")
a.erase(it);
it=a.begin();
while(it!=a.end())
{
cout<<"( "<<it->first<<", "<<it->second<<" ) ";
++it;
}
system("pause");
}
foochow 2005-07-31
  • 打赏
  • 举报
回复
// 16.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<map>
#include<iterator>
#include<string>
#include<algorithm>
using namespace std;
typedef pair<string, string> Pair;
int main()
{
multimap<string, string>a;
a.insert( Pair("name", "value1"));
a.insert( Pair("name", "value2"));
a.insert( Pair("name", "value3"));
pair<multimap<string, string>::iterator,multimap<string, string>::iterator>ps;
ps=a.equal_range("name");
multimap<string, string>::iterator it;
it=ps.first;
while(it!=ps.second)
{
if(it->second=="value2")a.erase(it++);
else it++;
}
system("PAUSE");
return 0;
}
Herro 2005-07-31
  • 打赏
  • 举报
回复
这样可以?erase只能删键值吧?

如果可以的话,还是有问题。。这样所有"value2"的都删了,而不是"name"下的"value2"
lw1a2 2005-07-31
  • 打赏
  • 举报
回复
a.erase( "value2");

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧