多图擦除不起作用
以下代码不适用于输入:
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;
}
following code doesn't work with input:
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
Why my erase doesn't work?
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你永远不会在循环中移动迭代器。
You never move the iterator it in your loop.