Urgent help!!! My programming crush after putting pop_back!

worked fine before I put popback... I need to finish it before 11... help ...


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
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <time.h>
#include <stdlib.h>
using namespace std;
void Remove(vector<int> &a);
int main()
{
	srand(time(0));
	vector <int> number;
	//get random number address 0 to 99 = 100 numbers
	for (int i = 0; i < 100; i++)
	{
		int a = rand() % 500 + 1;
		// put random number in each vector

		
		number.push_back(a);
               //erase duplicate...
             if (number[i] == number[i + 1])
		{
			number.pop_back();
			i--;
		}
		
	}
	//puts in order to lowest to highest
	sort(number.begin(), number.end());

	// send vector in address form (reference) 
	Remove(number);
	return 0;
}
void Remove(vector<int> &a)
{
	
	for (int i = 0; i < a.size(); i++)
	{
		if (i % 10 == 0)
		{
			cout << endl;
		}
		cout  << left << a[i] << " ";
	}
	cout << endl; 



}
Last edited on
You may want to check line 19. You are accessing a memory location greater then the length of the vector.
You are checking against number[i+1]. This will cause... problems. Think about it- you push back a at 0, then check of numbers[0] == numbers[1]... but what is numbers[1]?
Right... but how am I erase duplicate than...
Erase the duplicates after you have sorted. As stated in your previous question at http://www.cplusplus.com/forum/general/163153/ Maybe you should quickly reread it.
Last edited on
I run it on code block(win) and no error run ok,
but line no 22
if (number[i] == number[i + 1]) is not ok;
i+1 is show undefined no.
it should be i-1 for previous one.

Topic archived. No new replies allowed.