Remove integer from array

Ugghh C++! Im trying to search within an array for a number. If the number is in the array, I want to remove it. I get an error after I input the "checker". The error I get says Debug Assertion Failed, then i have to abort.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
int numbers=5, element, checker;
vector<int> group;
int count = 0, yescount = 0;
vector<int>::const_iterator iter;

while (count != numbers)
{
count++;
cout << "Enter numbers: ";
cin >> element;
group.push_back(element);
}

cout << "Please enter a number to search for within the array: ";
cin >> checker;


for (iter = group.begin(); iter != group.end(); ++iter)
{
if (*iter == checker)
group.erase(iter);
else
cout << "That number is not in the array";
}



system("pause");
return 0;
}
Last edited on
If you want to remove items from the middle of your container, you should consider a list instead of a vector
vector::erase invalidates iterators, but it returns a valid iterator to the next element of the vector http://www.cplusplus.com/reference/stl/vector/erase/
The equality operator is STILL ==. I thought you changed it?
And why don't you just erase iter?
Your right, i caught that, i changed it to ==... However I still get the same error, what do you mean just erase iter?
What Athar is saying is why not just do group.erase(iter); and get rid of yescount entirely instead of incrementing it separately.
ok so ive updated my code, shown above... Im still getting the Debug Assertion Failed, then i have to abort. I think im getting this when it tries to remove the selected number. If i enter a number thats not in the vector, then it outputs "That number is not in the array", the way it should.
It's because vector::erase invalidates all iterators, so if you erase something you can't keep going through your for loop.
...unless you do this:

1
2
3
4
5
for (iter = group.begin(); iter != group.end();)
{
   if (*iter == checker)iter=group.erase(iter);
   else ++iter;
}
Better yet, don't write your own loop at all. Simply use the remove_if algorithm.
http://cplusplus.com/reference/algorithm/remove_if/

Just for fun, here is an example.
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
// example that removes all values equal to some number. using remove_if and bind1st
int main ()
{
	int numbers=5, element, checker;
	std::vector<int> group;
	group.reserve(5); // reserve the 5 elements so that the capacity won't go beyond 5
	int count = 0, yescount = 0;
	std::vector<int>::const_iterator iter;

	while (count != numbers)
	{
		count++;
		std::cout << "Enter numbers: ";
		std::cin >> element;
		group.push_back(element);
	}

	std::cout << "Please enter a number to search for within the array: ";
	std::cin >> checker;

	// Print the original list
	std::cout << "original array\n";
	std::ostream_iterator<int> ostrmIter(std::cout, "\n");
	std::copy(group.begin(), group.end(), ostrmIter);

	// remove all elements with the value equal to checker from the array.
	group.erase(std::remove_if(group.begin(), group.end(), bind1st(std::equal_to<int>(), checker)),
		group.end());

	// print the final list
	std::cout << "array after removal of elements\n";
	std::copy(group.begin(), group.end(), ostrmIter);
	std::cout << std::endl;

	return 0;
}
Here is one with some error checking added in and the original input operation accepts any number of values that you wish to enter.
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
 #include <limits>

// example that removes all values equal to some number. using remove_if and bind1st
int main ()
{
	int element, checker;
	std::vector<int> group;

	// this loop will process as many numbers as it can.  It will end when there is no 
	// more data in the stream or when an error occurs.
	std::cout << "Enter a bunch of numbers separated by a space and"
		         " one non-numeric character when finished.\n";
	while(std::cin >> element)
	{
		group.push_back(element);
	}
	
	// clear the stream.
	std::cin.clear();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	std::cout << "Please enter a number to search for within the array: ";
	while( !(std::cin >> checker) )
	{
		std::cout << "That's not a number; ";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	// Print the original list
	std::cout << "original array\n";
	std::ostream_iterator<int> ostrmIter(std::cout, "\n");
	std::copy(group.begin(), group.end(), ostrmIter);

	// remove all elements with the value equal to checker from the array.
	group.erase(std::remove_if(group.begin(), group.end(), bind1st(std::equal_to<int>(), checker)),
		group.end());

	// print the final list
	std::cout << "array after removal of elements\n";
	std::copy(group.begin(), group.end(), ostrmIter);
	std::cout << std::endl;

	return 0;
}
Topic archived. No new replies allowed.