gpt4 book ai didi

c++ - multimap 删除不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:02:22 26 4
gpt4 key购买 nike

以下代码不适用于输入:

2
7
add Elly 0888424242
add Elly 0883666666
queryname Elly
querynum 0883266642
querynum 0888424242
delnum 0883666666
queryname Elly
3
add Kriss 42
add Elly 42
querynum 42

为什么我的删除不起作用?

#include<stdio.h>
#include<iostream>
#include<map>
#include <string>

using namespace std;

void PrintMapName(multimap<string, string> pN, string s)
{
pair<multimap<string,string>::iterator, multimap<string,string>::iterator> ii;
multimap<string, string>::iterator it;
ii = pN.equal_range(s);
multimap<string, int> tmp;
for(it = ii.first; it != ii.second; ++it)
{
tmp.insert(pair<string,int>(it->second,1));
}
multimap<string, int>::iterator i;
bool flag = false;
for(i = tmp.begin(); i != tmp.end(); i++)
{
if(flag)
{
cout<<" ";
}
cout<<i->first;
if(flag)
{
cout<<" ";
}
flag = true;
}
cout<<endl;
}

void PrintMapNumber(multimap<string, string> pN, string s)
{
multimap<string, string>::iterator it;
multimap<string, int> tmp;
for(it = pN.begin(); it != pN.end(); it++ )
{
if(it->second == s)
{
tmp.insert(pair<string,int>(it->first,1));
}
}
multimap<string, int>::iterator i;
bool flag = false;
for(i = tmp.begin(); i != tmp.end(); i++)
{
if(flag)
{
cout<<" ";
}
cout<<i->first;
if(flag)
{
cout<<" ";
}
flag = true;
}
cout<<endl;
}

void PrintFull(multimap<string, string> pN)
{
multimap<string, string>::iterator it;
for(it = pN.begin(); it != pN.end(); it++ )
{
cout<<"Key = "<<it->first<<" Value = "<<it->second<<endl;
}
}

int main()
{
multimap<string, string> phoneNums;
int N;
cin>>N;
int tests;
string tmp, tmp1,tmp2;
while(N > 0)
{
cin>>tests;
while(tests > 0)
{
cin>>tmp;
if(tmp == "add")
{
cin>>tmp1>>tmp2;
phoneNums.insert(pair<string,string>(tmp1,tmp2));
}
else
{
if(tmp == "delnum")
{ /////////////////////////////////////////HEREEEEEEE
multimap<string, string>::iterator it;
multimap<string, string>::iterator tmpr;
for(it = phoneNums.begin(); it != phoneNums.end();)
{
tmpr = it;
if(it->second == tmp1)
{
++tmpr;
if(tmpr == phoneNums.end())
{
phoneNums.erase(it,tmpr);
break;
}
else
{
phoneNums.erase(it,tmpr);
}
}
}
}
else
{
if(tmp == "delname")
{
cin>>tmp1;
phoneNums.erase(tmp1);
}
else
{
if(tmp =="queryname")
{
cin>>tmp1;
PrintMapName(phoneNums, tmp1);
}
else//querynum
{
cin>>tmp1;
PrintMapNumber(phoneNums, tmp1);
}
}
}
}
tests--;
}
N--;
}
return 0;
}

最佳答案

永远不要在循环中移动迭代器 it。

关于c++ - multimap 删除不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2534607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com