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.
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/
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.
// 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;
}
#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;
}