user12847218
user12847218

Reputation:

How to erase or change element while iterating over vector in C++?

I was in the middle of creating a simple sieve of Erathostenes function when I stumbled upon one obstacle. In to order to accomplish the highest efficiency in this task I wanted to use only a vector. Here is the current code:

vector<int> sieveOfErathostenes(int N) {

        vector <int> result(N, 1);

        for(int i = 2; i < sqrt(N); i++)

                if(result[i] == 1)

                        for(int j = 2*i; j < N; j += i)

                                result.at(j) = 0;
        //  :c
        return result;
}

This vector returns 1 and 0 in the proper position but I can't figure out how to implement both erasing or changing an element's value in a single loop. When I use an iterator to erase an element as in erase set element while iterating/// I can't access the vector to change its value, and when I use a standard for loop to access the element I can't remove it. I have tried going from the end of the vector and counting non zero elements and giving some offset when erasing but no success. TL DR: What I can't figure out is:

for(int i = 0; i < N; i++)
{
        if(result[i] == 0) {
                //remove at position i
        } else {
                result.at(i) = i;
        }
}

Thank you in advance for your time :)

Upvotes: 0

Views: 1359

Answers (2)

alex_noname
alex_noname

Reputation: 32083

If you still need to remove an element from a std::vector during traversal, keep in mind that erase returns an iterator following the last removed element:

  std::vector<int> result = {1,1,1,0,1,1,1};
  for(auto it = result.begin(); it != result.end(); )
  {
    if(*it==0)
      it = result.erase(it);
    else
      it++;
  }

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

Instead of erasing elements in the middle of the vector, you should write the results from the beginning of the vector and eliminate the unused elements in the end of vector.

int finalSize = 0;
for(int i = 0; i < N; i++)
{
        if(result[i] != 0) {
                result[finalSize++] = i;
        }
}
result.resize(finalSize);

Upvotes: 1

Related Questions