Vector Problem

Nov 21, 2016 at 12:05am
If the loop finds a repeated number, i want to delete that number. Im not sure what im doing wrong (i am assuming all numbers are sorted). It should delete the last '3' but im getting errors about variable 'x' and how it is uninitalized.

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
33
34
35
36
37
38
39
40
  #include <iostream>
#include <vector>

using namespace std;




int main() {


	unsigned int x;
	vector<int>number;
	number.push_back(1);
	number.push_back(2);
	number.push_back(3);
	number.push_back(3);
	
	
	for (unsigned int i = 0; i < number.size(); i++) {
		
			if (number[i] == number[i + 1]) {
				number[i + 1] =  x;
				number.erase(number.begin() + x - 1);
			}
		}
		


	
	


	
	
	system("pause");
	return 0;
}

Nov 21, 2016 at 12:46am
The compiler is telling you what to do.
unsigned int x{}; // initialised

Also, I think you meant line 22 to 25 to be
1
2
3
4
if (number[i] == number[i + 1]) {
    x = i + 1;
    number.erase(number.begin() + x);
}

Though, this would only remove duplicates that are adjacent.
Nov 21, 2016 at 2:02am
I get an error saying x was not declared in the scope when i put the curly braces on it.
Nov 21, 2016 at 2:11am
It is a piece of cake.
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
#include <iostream>
#include <vector>

using namespace std;

int main() 
{
	int x = 3;
	vector <int> number;
	number.push_back(1);
	number.push_back(2);
	number.push_back(3);
	number.push_back(3);

	unsigned int i;
	for(i = 0; i < number.size(); i++)
	{
		if(number[i] == x) 
		{
			number.erase(number.begin() + i); i--;
		}
		else cout << number[i] << endl;
	}
	
	system("pause");
	return 0;
}


http://cpp.sh/57qd


The code will delete all the numbers which is x (x is 3).
Nov 21, 2016 at 2:32am
Can you explain the i--; part?
Nov 21, 2016 at 3:03am
Im trying to make a more general formula and the problem i have now is that the vector subscript is out of range. Im pretty sure this is because of the number[x+1] but im not sure what to use in order to compare number[x] to what is directly after it.

this is what im stuck with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

	unsigned int i = 0;
	vector<int>number;
	number.push_back(1);
	number.push_back(2);
	number.push_back(3);
	number.push_back(3);
	number.push_back(4);
	
	
	for (unsigned int x = 0; x < number.size(); x++) {
		if (number[x] == number[x+1]) {
			
			number.erase(number.begin() + x);
			
			}
	}

Nov 21, 2016 at 3:05am
for (unsigned int x = 0; x < number.size() - 1; x++) {

However, if you have 3 in a row, you'll only remove one duplicate with the current code.
Last edited on Nov 21, 2016 at 3:06am
Nov 21, 2016 at 3:13am
Ok, but wouldnt that also delete the 4?
Nov 21, 2016 at 3:15am
Ok, but wouldnt that also delete the 4?

Why do you believe that would also delete the last element of the vector?
Nov 21, 2016 at 3:20am
I just ran it as cout<<number[x]; right after the if statement and it only prints out 123.
Nov 21, 2016 at 3:23am
I just ran it as cout<<number[x]; right after the if statement and it only prints out 123.

For the same reason your code would miss a duplicate if there were 3 in a row, it will not print 4 in the loop if you output from within (depending on where the output is placed.) Loop through the vector after the loop to see what is still in the vector.
Nov 21, 2016 at 3:28am
Thanks a lot that worked. I used the first loop to fix the vector, then i made a second one right after it that prints it out. It printed out 1234. Thanks again.
Topic archived. No new replies allowed.