help vector::erase understanding

So I want to use the vector::erase command to my remove contacts function, but I am kind of new to vectors and I need help understanding exactly how to do this.
I get if you already have a SET amount of vectors,
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3)
like so, but what if You want it to display the names to erase like my code below in the remove contacts function? What if I didn't have 3 elements in the vector? How do you give the user a option what vector to choose.

also, I am getting errors when I try to assign an ID to a vector element in addContact function.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
#include<string>
#include<vector>


using namespace std;
vector<string>Names;
vector<string>Phones;
vector<string>Addresses;

void addContact();
void searchByID();
void searchByName();
void removeContact();

int main()
{
		bool inMenu = true;
		int sel;
		while (inMenu) {
			cout << "Chose a Number to Execute the Selected Option: " << endl;
			cout << "[1]New Contact" << endl;
			cout << "[2]Search By Name" << endl;
			cout << "[3]Remove Contact" << endl;
			cout << "[4] Exit PhoneBook" << endl;
			cout << "Your choice: " << endl;
			cin >> sel;

			switch (sel)
			{

			case 1:
				addContact();
				break;
			case 2:

				searchByName();
				break;
			case 3:
				removeContact();
				break;
			case 4:
				inMenu = false;
				break;
			
			default:
				cout << "Does not recognice Selection";
				break;
			} // end of switch

		} // end of menu

		return 0;

	} // end of main 

void addContact()
{
	cin.clear();
	cin.sync();
	string name;
	string phone;
	
	cout << "Enter Contact Name: ";
	cin >> name;
	cout << "Enter Contact Phone Number: ";
	cin >> phone;
	Names.push_back(name);
	Phones.push_back(phone);
	
	
}
void searchByID()
{

	int value;
	cout << "Enter your Contact’s ID that you need to search: ";
	cin >> value;
	if (value > Names.size())
	{

		cout << "This ID does not exist";
		return; //terminates it
	}
	cout << "--Here is a little information on the contact ID--";
	cout << "Name: " << Names[value] << endl; //index is this and this is used to differentiate the values within the vector
	cout << "Phone Number: " << Phones[value] << endl;
	
}
void removeContact()
{

}
void searchByName()
{

	bool found = false; //bool is a data type that is only true or false, so this will tell us if the search was either successful or failed
	string name;
	cout << "Enter the Contact Name to search: ";
	cin >> name;
	for (int i = 0; i != Names.size(); i++)
	{
		if (Names[i] == name)
		{

			cout << "Name:" << Names[i] << endl;
			cout << "Phone:" << Phones[i] << endl;
			found = true;

		}

	}
	if (!found)
	{
		cout << "Contact was not found" << endl;
	}
}


Last edited on
trying to do something like this kind of like the search by name, but deleting it by index but its erroing up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void removeContact()
{
	bool found = false; //bool is a data type that is only true or false, so this will tell us if the search was either successful or failed
	string name;
	cout << "Enter the Contact Name to delete: ";
	cin >> name;
	for (int i = 0; i != Names.size(); i++)
	{
		if (Names[i] == name)
		{

			Names[i].erase << endl;
			Phones[i].erase << endl;
			found = true;
			cout << "Contact Erased";

		}

	}
	if (!found)
	{
		cout << "Contact was not found" << endl;
	}
}
The erase function of a vector takes in an iterator. Phones.begin() is an
example of an iterator. Adding a number to Phones.begin() will also get you an
iterator.

1
2
3
4
5
6
7
8
9
10
11
12
for (size_t i = 0; i != Names.size(); i++)
{
  if (Names[i] == name)
  {
    Names.erase(Names.begin()+i);
    Phones.erase(Phones.begin()+i);
    found = true;
    cout << "Contact Erased";
    break;  // Must break out of loop if found
  }

}

One bug fix to fiji885's solution:
for (size_t i = 0; i < Names.size(); i++) // change != to <
Without this the loop will continue if you delete the last item.

In the original program:
Line 79 should be if (value >= Names.size())

Have you learned about classes and structures? If so then it would be better to put all info about a person in a Person struct and then have a single vector:
1
2
3
4
5
struct Person {
    string name, phone;
};

vector<Person> people;


Also, a couple utility functions will let you remove redundant code. See showContact() and findContact() below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<string>
#include<vector>

using namespace std;
vector < string > Names;
vector < string > Phones;
vector < string > Addresses;

void addContact();
void searchByID();
void searchByName();
void removeContact();

int
main()
{
    bool inMenu = true;
    int sel;
    while (inMenu) {
	cout << "Chose a Number to Execute the Selected Option: " << endl;
	cout << "[1]New Contact" << endl;
	cout << "[2]Search By Name" << endl;
	cout << "[3]Remove Contact" << endl;
	cout << "[4] Exit PhoneBook" << endl;
	cout << "Your choice: " << endl;
	cin >> sel;

	switch (sel) {

	case 1:
	    addContact();
	    break;
	case 2:

	    searchByName();
	    break;
	case 3:
	    removeContact();
	    break;
	case 4:
	    inMenu = false;
	    break;

	default:
	    cout << "Does not recognice Selection";
	    break;
	}					 // end of switch

    }						 // end of menu

    return 0;

}						 // end of main 

// Prompt for a name and search for it in the Names vector. Return
// The index if found, or Names.size() if not
unsigned
findByName()
{
    string name;
    cout << "Enter the Contact Name to search: ";
    cin >> name;
    unsigned i;
    for (i = 0; i != Names.size(); i++) {
	if (Names[i] == name) {
	    break;
	}
    }
    return i;
}

// Given an index, display info about a contact
void
showContact(unsigned idx)
{
    cout << "Name:" << Names[idx] << endl;
    cout << "Phone:" << Phones[idx] << endl;
}

void
addContact()
{
    cin.clear();
    cin.sync();
    string name;
    string phone;

    cout << "Enter Contact Name: ";
    cin >> name;
    cout << "Enter Contact Phone Number: ";
    cin >> phone;
    Names.push_back(name);
    Phones.push_back(phone);
}

void
searchByID()
{
    unsigned value;
    cout << "Enter your Contact’s ID that you need to search: ";
    cin >> value;
    if (value >= Names.size()) {

	cout << "This ID does not exist";
	return;					 //terminates it
    }
    cout << "--Here is a little information on the contact ID--";
    showContact(value);
}

void
removeContact()
{
    unsigned i = findByName();
    if (i < Names.size()) {
	Names.erase(Names.begin() + i);
	Phones.erase(Phones.begin() + i);
	cout << "Contact Erased\n";
    } else {
	cout << "Contact was not found\n";
    }
}

void
searchByName()
{
    unsigned i = findByName();
    if (i < Names.size()) {
	showContact(i);
    } else {
	cout << "Contact was not found" << endl;
    }
}
Topic archived. No new replies allowed.