How to remove a value from vector

closed account (3vX4LyTq)
I have an exercise that is asking me to make a vector, fill it with 20 integers from 1-20 and then print it. Next, check to find repeat integers and then remove them.

The method that I think that I am going to use is to find an integer, then check every integer in the vector. If there is another integer with the same value then I will get rid of that, then go to the next integer.

I'm really unsure of how to go about this. Any help is appreciated. Here's what I have so far, but it's not working.

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
 #include <iostream>
#include <stdlib.h>
#include <vector>
#include <time.h>
using namespace std;
int main() {
    srand(time(NULL));
    vector<int> List;
    cout << "Here is Santa's list!\n";
    
    for(int i = 0; i<20; i++){
      List.push_back(rand() % 20 + 1);///Fills vector with 20 random integers
      cout << List[i] << " ";///Prints the vector
    }
    
    vector<int> Revision(List.begin(), List.end());
    
    cout << "\n Maybe Santa made a mistake! Here is the revised version. \n";
    
    ///Now, I should revise the list to remove repeat integers i.e. if there are multiple '4's, then skip the rest of the fours
    
    for(int n = 0; n<20; n++){
      for(int i = 0; i<20; i++){
        if(Revision[i] == n && i == 1){
          Revision.erase(Revision.begin());
        }
        if(Revision[i] == n){
          Revision.erase(Revision.begin() + (i-1));///Removes that element
        }
      }
    }
      for(int i = 0; i<20; i++){///Prints the list
        cout << Revision[i] << " ";
      }
     
    
    
}


PS Any formatting advice would be helpful as well
Last edited on
See here for 3 different approaches to erase duplicates from vector and their performance stats:
http://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector
Also, avoid rand(): search this forum for the last week or so for reasons why and other alternatives
Any formatting advice would be helpful as well

google allman style - i find it the cleanest though its a matter of opinion of course
Also the header files ending .h are C-style header files, their equilvalents drop the .h and stick a c in front, so ctime instead of time.h, etc
Finally using namespace std?http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Topic archived. No new replies allowed.