Vectors - Deleting all negative elements using pop_back and push_back

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  int vectorLength = 10;

  vector<int> 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<int> 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> seq { -34, -21, 5, 72, -2, 17 } ;
    std::vector<int> 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():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>

void remove_negatives( std::vector<int>& 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<int> 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?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
	int vectorLength = 10;

	vector<int> 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<int> noNegs;

	////////////////////////////////////////////

	for(unsigned int i=0; i<bothSigns.size(); i++)
	{
		if(bothSigns[i] >= 0)
		{
			noNegs.push_back(bothSigns[i]);	
		}
	}

	for(unsigned int i=0; i<noNegs.size(); i++)
	{
		cout<<noNegs[i]<<endl;
	}

	std::cin.get();



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.