views:

138

answers:

1

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;
}
+1  A: 

You never move the iterator it in your loop.

Julio Guerra