### Vectors - Deleting all negative elements using pop_back and push_back

 ``1234567891011121314151617181920212223242526272829303132`` `````` int vectorLength = 10; vector bothSigns(vectorLength); cout << " Input vector: "; for (int i = 0; i < vectorLength; i = i + 1) { bothSigns[i] = rand()%201 - 100; cout << "\t" << bothSigns[i]; } cout << "\n"; vector noNegs; //////////////////////////////////////////// noNegs = bothSigns; for(int i = 0; i < vectorLength; i = i + 1) { if(noNegs[i] < 0) { int j = i; while(noNegs[j] < 0) { j = j + 1; cout << endl << endl << "I: " << i << endl << "J: " << j << endl; } for(j; j < vectorLength - 1; j = j + 1) { noNegs[i] = noNegs[j]; } noNegs.pop_back(); } }``````

I'm getting incorrect outputs, obviously. Basically my idea of the code was look at each element, and if that element was less than 0, you look for the next non-neg element and replace it with that. Maybe my code doesn't do what I think? Is there a better way of going about this all together?

Correct Example: Beforehand Output: -34 -21 5 72 -2 17
Afterhand Output: 5 72 17
Last edited on
std::remove_if
we can't use std::remove_if, only pop_back and push_back. I wish it were that simple.
 ``1234567891011121314151617`` ``````#include #include int main() { std::vector seq { -34, -21, 5, 72, -2, 17 } ; std::vector temp ; for( int v : seq ) if( v >= 0 ) temp.push_back(v) ; while( !seq.empty() ) seq.pop_back() ; // seq.clear() for( int v : temp ) seq.push_back(v) ; for( int v : seq ) std::cout << v << ' ' ; std::cout << '\n' ; }``````

http://coliru.stacked-crooked.com/a/1cc5ae8c51f5e974
the only vector class function i can use is pop_back and push_back. also can't declare any new vectors. haven't learned std:
> the only vector class function i can use is pop_back and push_back.

That is naive.

1. Before we can use pop_back() at all, we need to make sure that the vector is not empty.

2. If we can't use any other function, then we also can't examine the value of any element in the vector.
How then are we going to know if a value is negative?

Using empty() and back() in addition to pop_back() and push_back():

 ``123456789101112131415161718192021222324`` ``````#include #include void remove_negatives( std::vector& seq ) { if( !seq.empty() ) { const int v = seq.back() ; seq.pop_back() ; remove_negatives(seq) ; if( v >= 0 ) seq.push_back(v) ; } } int main() { std::vector seq { -34, -21, 5, 72, -2, 17 } ; remove_negatives(seq) ; for( int v : seq ) std::cout << v << ' ' ; std::cout << '\n' ; }``````

http://coliru.stacked-crooked.com/a/c9f3f88f0d4b2977
I know it makes it difficult. Everything after the comment marks is my code, everything before was given and can not be changed.
Hi Dealaus,

perhaps I am overlooking something and if so feel free to tell me, but is there a reason why this would not be acceptable?:

 ``12345678910111213141516171819202122232425262728`` `````` int vectorLength = 10; vector bothSigns(vectorLength); cout << " Input vector: "; for (int i = 0; i < vectorLength; i = i + 1) { bothSigns[i] = rand()%201 - 100; cout << "\t" << bothSigns[i]; } cout << "\n"; vector noNegs; //////////////////////////////////////////// for(unsigned int i=0; i= 0) { noNegs.push_back(bothSigns[i]); } } for(unsigned int i=0; i

Thanks -NGangst
Last edited on
Why not only add them to the vector if they are positive? use a while loop instead of for loop.
Topic archived. No new replies allowed.