Problems erasing a Vector in a map

I am creating an address book in which the user can input a name and unlimited email addresses for each name. I have everything working except the ability to delete an address. I have used a map<string,emailStore> where emailStore is a string vector. the map name is name_to_email, and the initialized vector is book.
The program compiles and runs, but doesn't actually erase the value, what am I missing? Thanks in advance for any insight.

void delEmailAddressmanager()
{

cout << "What email address do you wish to delete?" ;
cin >> address;

for(map<string, emailStore>::iterator it = name_to_email.begin(); it !=name_to_email.end(); it++)

{
for(int j = 0 ; j < it->second.size(); j++)
{
if (it->second[j].c_str() == address)
{
book.erase(book.begin()+j);
break;
}
}
cout <<endl <<endl;
}
}
code tags please.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void delEmailAddressmanager()
{

     cout << "What email address do you wish to delete?" ;
     cin >> address;

     for(map<string, emailStore>::iterator it = name_to_email.begin(); it  !=name_to_email.end(); it++)

       { 
          for(int j = 0 ; j < it->second.size(); j++)
           {
              if (it->second[j].c_str() == address)
               {
                  book.erase(book.begin()+j); //My Problem Line
                  break;
                 }
            }
            cout <<endl <<endl;
        }
}


Is this what you meant?
Last edited on
Yes. Thanks.

Where are "name_to_email", "address" and "book" defined?

How does "book" relate you "it->second" on line 12?

I think you may want to do "name_to_email.erase(it)" there instead. But I cannot tell for sure without more context.
Yes indeed, how does book relate to any vector in your map, let alone the correct one?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void delEmailAddressmanager()
{
	cout << "What email address do you wish to delete?" ;
	cin >> address;

	for(map<string, emailStore>::iterator it = name_to_email.begin(); it != name_to_email.end(); ++it)
	{
		 for(unsigned int j = 0 ; j < it->second.size(); ++j)
		{
			 if (it->second[j].c_str() == address)
			{
				it->second.erase(it->second.begin() + j); //Try this?
				break;
			}
		}
		cout << endl <<endl;
	}
}
Is this it?

Also, please do not indent with spaces, it's tiresome and tedious. Use tabs instead ;)
Last edited on
What are you comparing here?
 
if (it->second[j].c_str() == address)

What is the type of address? If address is a std::string then you need not be using c_str() to get a char* to compare:
 
if (it->second[j] == address)

However if address is a char* then your compare won't work. You need strcmp(). But use strings.

Also if you are using erase, then you may as well be iterating through your vector rather than indexing it:
 
for(std::vector<std::string>::iterator vi = it->secong.begin();vi != it->second.end(); ++vi)

That leaves you with a nice iterator for your erase instruction.


Last edited on
And maybe it shouldn't be the address book the one who handles all that.
1
2
3
for(int j = 0 ; j < it->second.size(); j++)
   if ( it->second.erase(address) ) //let the emailStore to handle the modification of a emailStore
      break;
LB you have saved the day! book is the vector used in my map, address is what the user was inputing to have deleted. So I was thinking that while iterating through the second value of the map(the vector) that meant I would have to delete from book and not the map. I had tried "name_to_email.erase(it)" but with no success. Thanks for all your help!
Topic archived. No new replies allowed.