Removing multiple elements and keeping order

I have written a program that will remove value in the location as given by the user in my vector; but, I misunderstood what I needed to do with my function.
I need to have it iterate through my vector and remove any instances of the number provided by the user.
I feel I could adjust what I have slightly to make this work if it was just one instance of the number.
Would my function need to be coded differently if the number given by the user occurs more than once?
I can make the elements shift to the left and maintain the order when it is just one location being removed; but, I am second guessing myself as to what to do if its more than one number being removed.
What do I need to do differently?
Here is what I have:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

vector <int> create(int size);
void storeRandom(vector<int> & );
void output (const vector<int> &);
void deletionO(vector<int>& v, int loc);

int main(int argc, char** argv) {

    srand(time(0));
    //Vector function
    cout << "Creating vector: " << endl;
    vector<int> v = create(10);
    cout << "Storing values in vector: " << endl;
    storeRandom(v); 
    cout << "Outputting vector: " << endl;
    output(v);
    
    //Delete element from given location in vector
    cout << "Please enter the location of the value you'd like to delete from "
            "the vector: ";
    int loc;
    cin >> loc;
    deletionO(v, loc);
    output(v);
    
    return 0;
}

/**
 * Creates and returns a vector of the given size with all 0's.
 * @param size
 * @return 
 */
vector<int> create(int size)
{
    vector<int> newVector;
    
    for(int i = 0; i < size; i++)
    {
        newVector.push_back(0);
    }
    return newVector;
}
/**
 * Function stores random values between 0 - 100
 * @param v
 */
void storeRandom(vector<int>& v)
{
    for(int i = 0; i < v.size(); i++)
    {
        v[i] = rand() % 101;
    }
}
/**
 * Outputs a vector in horizontal format 
 * @param v
 */
void output(const vector<int>& v)
{
    for(int i = 0; i < v.size(); i++)
    {
        cout << v[i] << "  ";
    }
    cout << endl;
}

void deletionO(vector<int>& v, int loc)
{
    if(loc < 0 || loc > v.size()) return;
    for(int i = loc; i <= v.size() - 1; i++ ) 
    {
        v[i] = v[i + 1];
    }
    v.pop_back();
}


The part I think I'll need to adjust is just the last function and then I'll fix my cout statements and all that
A key part of programming is using functions as building blocks.

Make a new function that looks through a vector for a number to remove, and if you find it, call the deletionO function to remove the element at that location. Then, wind location back one (because you moved everything after that location one to the left), and keep looking through the vector for the number to remove. If you find it, call the deletionO function to remove the element at that location. Then, wind location back one (because you moved everything after that location one to the left), and keep looking through the vector....

This would be a simple addition to your code, in which you call the deletionO function you've already created.
Last edited on
Thank you, I am working on writing the new function now.
I believe I see how to do this the only thing I'm confused about is when you say to wind location back one, do I want to do this inside the for loop after the deletionO function is called? My best guess as to what to do is to subtract one from the location; but, I am not entirely sure if that is correct
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    srand(time(0));
    //Vector function
    cout << "Creating vector: " << endl;
    vector<int> v = create(10);
    cout << "Storing values in vector: " << endl;
    storeRandom(v); 
    cout << "Outputting vector: " << endl;
    output(v);
    
    //Delete element from given location in vector
    cout << "Please enter the value you'd like to delete from the vector: ";
    int val;
    cin >> val;
    removeVal(v, val);
    output(v);
    
    return 0;
}

/**
 * Creates and returns a vector of the given size with all 0's.
 * @param size
 * @return 
 */
vector<int> create(int size)
{
    vector<int> newVector;
    
    for(int i = 0; i < size; i++)
    {
        newVector.push_back(0);
    }
    return newVector;
}
/**
 * Function stores random values between 0 - 100
 * @param v
 */
void storeRandom(vector<int>& v)
{
    for(int i = 0; i < v.size(); i++)
    {
        v[i] = rand() % 11;
    }
}
/**
 * Outputs a vector in horizontal format 
 * @param v
 */
void output(const vector<int>& v)
{
    for(int i = 0; i < v.size(); i++)
    {
        cout << v[i] << "  ";
    }
    cout << endl;
}

void deletionO(vector<int>& v, int loc)
{
    if(loc < 0 || loc > v.size()) return;
    for(int i = loc; i <= v.size() - 1; i++ ) 
    {
        v[i] = v[i + 1];
    }
    v.pop_back();
}

void removeVal(vector<int>& v, int val)
{
    for(int i = 0; i < v.size(); i++)
    {
        if(v[i] == val)
        {
            deletionO(v, val);
        }
    }
}

Here is what I have so far, I'm not sure how to make the relation of the value given by the user and the locations it may be in the vector
Last edited on
Topic archived. No new replies allowed.