I have some code: I need to delete duplicates, I don't really know which step im going wrong at. kinda been losing sleep over this. i feel like im needlessly overcomplicating this but too brain dead atm to see where i went wrong.
STILL HAVING ISSUES =(
I'm making the changes I incoorporated based on suggestions... still running into out of bounds even though my size for the arr is 8... I've tried printing values, it seems to break unexpectedly on the 3rd element but I dont know what to do. any other help would be appreciated...
template <typename T>
void removeDup(std::vector<T> & v)
{
int last = v.size()-1;
for(int i = 0; i <= v.size(); i++)
{
if (count(v, i, last, v[i]) > 1){ // if there is more than 1
v.erase(v.begin()+(i)); // erase it
}
}
}
template <typename T>
int count(const std::vector<T> & v, int first, int last, const T& target){
int index = first;
int count = 0;
for (index; index < last; index++){
if (v[index] == target){
count++;
}
}
return count;
}
template <typename T>
int seqVectSearch(const std::vector<T> & v, int first, int last, const T& target){
int index = first;
int returnVal = -1;
for (index; index < last; index++){
if (v[index] == target){
returnVal = index;
}
}
return returnVal;
}
template <typename T>
void writeVector(const std::vector<T> & v){
int i;
int n = v.size();
for (i = 0; i < n; i++)
std::cout << v[i] << ' ';
std::cout << std::endl;
}
template void removeDup(std::vector<int> & v);
template int seqVectSearch(const std::vector<int> & v, int first, int last, const int& target);
template void writeVector(const std::vector<int> & v);
template int count(const std::vector<int> & v, int first, int last, const int& target);
Output:
Testing removeDup
Original vector is 1 7 2 7 9 1 2 8 9
Vector with duplicates removed is 1 2 7 9 1 2 8 9
1 2 7 9 1 2 8 9
Press any key to continue . . .