I have a vector I need to delete every third element
123456
12456
1245
125
15
1

vector<int>::iterator iter = v.begin();
    while( iter != v.end() )
	
	for( int i = 0; i < v.size() +2 ; i++ )
    while( iter != v.end() )
    {
      if (v.size() != 1 ) )
        iter = v.erase( iter );
      else
        ++iter;
    }
 
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, ""));

any suggestion it only gives the first two elements. Thanks in advance!

Recommended Answers

All 4 Replies

Can't grok this mysterious code...
Keep it simple:

while (it != v.end()) {
        if (++it != v.end() && ++it != v.end())
            it = v.erase(it);
    }
//declare and initialize variables
iterator current = vector.begin
iterator stop = vector.end 
int n = 0;

//loop de loop
while(size of vector not equal one)
{
    ++n;
    ++current;

    if(current equals stop) //reset current to beginning of vector
         current = vector.begin
    
     if n equals 3
     {
         erase current 

         //reset variables
         stop = vector.end
         n = 0
     }
}

it's not working the code still gives two values

It works fine.
Try to print vector with regular for loop:

for (size_t i = 0; i < v.size(); i++)
    cout << v[i] << '\n';

and see what happens.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.