Erase vector items exercise

Hi everyone. I am a student learning C++. I have a the following assignment that I can't get to compile. Does anyone offer any hints as to why? This is not for a grade, rather discussion.

/*
Write a function called Erase_Vector_Items. This function will take as input, 2 parameters:

1. A vector
2. The item to erase from the vector (if it exists)

The post-condition after this function is that no element within the vector will contain this item.

Erase_Vector_Items needs to work with vectors of ANY data-type.

Write a separate function that will output the contents of the vector (of ANY data-type).

Demonstrate both functions working with 2 different data-types.

*/

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
#include <vector> // vector class-template definiton
#include <iostream> // preprocessing directive

using namespace std;

template<class T> <T>
void Erase_Vector_Items(vector<T> &v, T item) {
    int i;

    for (i = 0; i < v.size(); i++) {
        if (v[i] == item) {
            v.erase(v.begin() + i);
            i--;
        }
    }
}

template<class T> <T>
void Display_1d_Vector(vector<T>& v) {
     for (i = 0; i < v.size(); i++) {
            cout << v[i] << " ";
     }
}

int main() {

    vector<int> numbers;
    numbers.push_back(12);
    numbers.push_back(111);
    numbers.push_back(12);
    Erase_Vector_Items(numbers, 12);
    Display_1d_Vector(numbers);
          
    vector<char> letters;
    letters.push_back('a');
    letters.push_back('b');
    letters.push_back('a');
    Erase_Vector_Items(letters, 'a');
    Display_1d_Vector(letters);
    
    return 0;
}
#endif 
Last edited on
1
2
3
template <typename T>
void Erase_Vector_Items(std::vector<T> &xs, T const& x)
{ xs.erase(std::remove(xs.begin(), xs.end(), x), xs.end()); }
Last edited on
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
#include <vector> // vector class-template definiton
#include <iostream> // preprocessing directive

using namespace std;

template<class T> // <T>
void Erase_Vector_Items(vector<T> &v, T item) {
    //int i;

    for ( size_t i = 0; i < v.size(); i++) { // <--
        if (v[i] == item) {
            v.erase(v.begin() + i);
            i--;
        }
    }
}

template<class T> // <T>
void Display_1d_Vector(vector<T>& v) {
     for (size_t i = 0; i < v.size(); i++) { // <--
            cout << v[i] << " ";
     }
}

int main() {

    vector<int> numbers;
    numbers.push_back(12);
    numbers.push_back(111);
    numbers.push_back(12);
    Erase_Vector_Items(numbers, 12);
    Display_1d_Vector(numbers);
          
    vector<char> letters;
    letters.push_back('a');
    letters.push_back('b');
    letters.push_back('a');
    Erase_Vector_Items(letters, 'a');
    Display_1d_Vector(letters);
    
    return 0;
}
// #endif 


111 b  
Exit code: 0 (normal program termination)
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
#include <iostream>
#include <vector>
#include <algorithm>

template <typename T, typename A > // note: type A is the allocator for the vector
void Erase_Vector_Items( std::vector<T,A>& vec, const T& value ) {

    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    for( auto iter = vec.begin() ; iter != vec.end() ; ) {

        if( *iter == value ) { // erase this item

            // erase returns an iterator to the item immediately after
            // the item that was removed (or end if there are no more items)
            // update our iterator to refer to the next item
            // note: we need to use a construct like this because erase
            // "Invalidates iterators and references at or after the point
            // of the erase, including the end() iterator."
            // https://en.cppreference.com/w/cpp/container/vector/erase
            iter = vec.erase(iter) ;
        }

        else // do not erase this item
            ++iter ; // / update our iterator to refer to the next item
    }
}

// or idiomatic: use the erase-remove idiom
// https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
template <typename T, typename A > // note: A is the allocator for the vector
void Erase_Vector_Items_2( std::vector<T,A>& vec, const T& value ) {

        // https://en.cppreference.com/w/cpp/algorithm/remove
        vec.erase( std::remove( vec.begin(), vec.end(), value ), vec.end() ) ;
}

// Write a separate function that will output the contents of the vector (of ANY data-type).////
template <typename T, typename A > // note: A is the allocator for the vector
void Print_Vector_Items( const std::vector<T,A>& vec ) {

    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& v : vec ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}

int main() {

    // initialiser list: http://www.stroustrup.com/C++11FAQ.html#init-list
    std::vector<int> vec { 5, 4, 3, 5, 4, 7, 6, 9, 5, 4, 4, 4, 5 } ;
    Print_Vector_Items(vec) ;

    Erase_Vector_Items( vec, 5 ) ;
    Print_Vector_Items(vec) ;

    Erase_Vector_Items_2( vec, 4 ) ;
    Print_Vector_Items(vec) ;
}

http://coliru.stacked-crooked.com/a/8ae2517ea1867a88
The Nun flies in to mollycoddle - how fatuous is that!
lucretia borgese reports again from the nunnery fatuaosa
Topic archived. No new replies allowed.