多图擦除不起作用

发布于 2024-08-26 21:17:28 字数 4370 浏览 8 评论 0原文

以下代码不适用于输入:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放肆 2024-09-02 21:17:28

你永远不会在循环中移动迭代器。

You never move the iterator it in your loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文