user3573663
user3573663

Reputation: 1

std::remove vector for specified element

I'm trying to remove a specific value that is defined by an if statement, then stores as an int, i want to then look through the vector and erase it by using

if (comparedValuesBetween2[i] == First0ValueFor0Number2[j])
{
    //make sure if there are no values to compare left just leave the the while loop by editing the count.
    comparedValuesBetween2.resize(std::remove(comparedValuesBetween2.begin(), comparedValuesBetween2.end(), 8) - comparedValuesBetween2.begin());
 }

but im getting these errors and i dont know why if you could help

6 IntelliSense: too many arguments in function call g:\08227 acw\ACW\Sudoku\Sudoku\main.cpp 225

5 IntelliSense: no suitable conversion function from "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>" to "const char *" exists g:\08227 acw\ACW\Sudoku\Sudoku\main.cpp 225

I'm very new to c++. Thanks for your help.

Upvotes: 0

Views: 399

Answers (3)

Joky
Joky

Reputation: 1628

You don't really provide enough information on what you are trying to achieve. I suppose that i and j are loop indices?

The "idiomatic" way of doing it is called remove/erase idiom:

for(int j; .....) {
   ... 
   if(....) {
      comparedValuesBetween2.erase(std::remove(comparedValuesBetween2.begin(), comparedValuesBetween2.end(), First0ValueFor0Number2[j]));
   }
}

It has to be refined depending on what exactly is your use-case. Ideally the loop on j should not be a raw loop as well.

Upvotes: 0

tsragravorogh
tsragravorogh

Reputation: 3173

Also, just a side note, vector.erase() returns an iterator that points the next element in the vector. So if you are traversing your vector through an iterator, you gotta make sure you don't loose track of the iterator after you delete an element in the vector.

Upvotes: 0

herohuyongtao
herohuyongtao

Reputation: 50717

You can simply call std::vector::erase() to remove specified element from the container:

if (comparedValuesBetween2[i] == First0ValueFor0Number2[j])
    comparedValuesBetween2.erase(comparedValuesBetween2.begin() + i);

Upvotes: 1

Related Questions