Can`t erase elements from vectors

closed account (EwCjE3v7)
Hello there,
So I have been working on this calculator for a while just trying to make it better, today I took away some if statements and added a while loop so it keeps going, but I get this error
No matching funtions call to 'std::vector<int>::erase()
. The line number is 78.

Edit: Fixed it a bit, I was passing functions not iterators, it runs but crashes when I give the input of 5+5+5.
Code: http://codeviewer.org/view/code:3baa


Can someone fix this: http://www.cplusplus.com/reference/vector/vector/erase/

It has false info, it does what I did. Thats where I got the info
Last edited on
What false info?
Iterators, pointers and references pointing to position (or first) and beyond are invalidated


That includes your ivend, cvend, etc.
closed account (EwCjE3v7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// erasing from vector
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;

  // set some values (from 1 to 10)
  for (int i=1; i<=10; i++) myvector.push_back(i);

  // erase the 6th element
  myvector.erase (myvector.begin()+5);

  // erase the first 3 elements:
  myvector.erase (myvector.begin(),myvector.begin()+3);

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size(); ++i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}


look at myvector.erase (myvector.begin()+5); and myvector.erase (myvector.begin(),myvector.begin()+3);

Edit:

Anyone who can help me?
Last edited on
Line 15: getline requires the string header.
Line 17: since you didn't include the <string> header, input.empty() is flagged as an error.

Some implementations may include <string> from inside <iostream>, but there is no guarantee that all implementations do this. Therefore, it is best practice that if you reference functions from <string>, that you explicity include <string>.

Please post your code using the code tag facility on this site. The code you linked was not easily copied and pasted into a compiler.

Last edited on
closed account (EwCjE3v7)
First sorry for posting there but there is a limit here and once when I edited the code and wrote it said I passed that so thats why :D

Added the string header but it still crashed on second input :( first workd

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
82
83
84
85
86
#include <iostream>
#include <vector>
#include <string>
using std::cout; using std::cin; using std::cerr; using std::endl;
using std::vector; using std::string;

int main()
{
    string input; // for gettin our input
    vector<int> iv; // for holding our numbers that we are going to calculate
    vector<char> cv; // for checking if user wants to +,-,*,/
    int answer = 0, counter = 0; // For holding the answer
    auto cvbegin = cv.begin(), cvend = cv.end();
    auto ivbegin = iv.begin(), ivend = iv.end();
    cout << "Type your calculations, example \"5+5-2*6/4\" (Spaces are allowed) : "; // tell user how the calculator works
    while (getline(cin, input)) { // get input into the string input, whitespaces are included

        if (!input.empty()) { // make sure we get input
            for (auto c : input) { // if we get input then check the characters in input
                if (isdigit(c)) { // if the character is a digit then
                    iv.push_back(c - '0'); // place it at the end of the int vector iv
                }
                else if (ispunct(c)) { // if its a puntuation then
                    if (c == '+' || c == '-' || c == '*' || c == '/') { // check if its a puntuation mark that we can process
                        cv.push_back(c); // put it at the end of the char vector
                    } else {
                        cerr << "Sorry character/sign not known: '" << c << "'. Only +,-,*,/." << endl; // tell user that the character/sign is not known
                        return -1; // return failiure
                    }
                }
            }

            answer = 0, counter = 0, cvbegin = cv.begin();

            while (*cvbegin == '+') {
                if (counter == 0) {
                    answer = iv[0] + iv[1];
                    cout << iv[0] << " + " << iv[1] << " = " << answer << endl;
                    ++cvbegin, counter = 2;
                } else {
                    cout << answer << " + " << iv[counter] << " = " << answer + iv[counter] << endl;
                    answer = answer + iv[counter];
                    ++cvbegin, ++counter;
                }
            }
            while (*cvbegin == '-') {
                if (counter == 0) {
                    answer = iv[0] - iv[1];
                    cout << iv[0] << "- " << iv[1] << " = " << answer << endl;
                    ++cvbegin, counter = 2;
                } else {
                    cout << answer << " - " << iv[counter] << " = " << answer - iv[counter] << endl;
                    answer = answer - iv[counter];
                    ++cvbegin, ++counter;
                }
            }
            while (*cvbegin == '*') {
                if (counter == 0) {
                    answer = iv[0] * iv[1];
                    cout << iv[0] << " * " << iv[1] << " = " << answer << endl;
                    ++cvbegin, counter = 2;
                } else {
                    cout << answer << " * " << iv[counter] << " = " << answer * iv[counter] << endl;
                    answer = answer * iv[counter];
                    ++cvbegin, ++counter;
                }
            }
            while (*cvbegin == '/') {
                if (counter == 0) {
                    answer = iv[0] / iv[1];
                    cout << iv[0] << " / " << iv[1] << " = " << answer << endl;
                    ++cvbegin, counter = 2;
                } else {
                    cout << answer << " / " << iv[counter] << " = " << answer / iv[counter] << endl;
                    answer = answer / iv[counter];
                    ++cvbegin, ++counter;
                }
            }
            iv.erase(ivbegin, ivend-1), cv.erase(cvbegin, cvend-1);
        } else { // If we get no input then...
            cerr << "No input, well bye." << endl; // Tell user that there was no input
            return -1; // return failiure
        }
    }
    return 0;
}
I ran your program and get the following output:

myvector contains: 4 5 7 8 9 10

v[5] = 6 was removed as expected.
v[0] = 1, v[1] = 2, v[2]=3 were also removed as expected.

What were you expecting?
Last edited on
closed account (EwCjE3v7)
I need to erase all elements from both vectors
Then use myvector.clear();.
You invalidate the iterators with the `.push_back()'


iv.erase(ivbegin, ivend-1) ¿why `-1'?

To erase all the elements, you could create the vectors inside the loop, so they are destroyed each time.
closed account (EwCjE3v7)
I donno, I got the info from http://www.cplusplus.com/reference/vector/vector/erase/
Can anyone fix it, ive tried everything :(
¿have you tried `.clear()'?
¿have you tried to limit the scope?
¿have you tried to call `.begin()' and `.end()' instead of use invalid catch values?

You've tried nothing, CaptainBlastXD
closed account (EwCjE3v7)
.clear will only clear,
2. i donno what u mean, but if u mean limit the elements, than no and i dont really want to :D
3. sorry what invalid catch values?


What do u mean ive tried nothing, ive tried everything.
Let me upload my updated code that i wrote now which has the same prob :(
.clear will only clear,

What does that mean? clear() erases all elements. Is that not what you wanted?

As far as limiting the scope, if you declare your vectors inside the loop, then when the loop loops back around, those vectors will be automatically destroyed, so you'll start the next loop iteration with a fresh new (empty) set of vectors.
> I need to erase all elements from both vectors
http://www.cplusplus.com/reference/vector/vector/clear/
Removes all elements from the vector (which are destroyed)

Limiting scope
1
2
3
4
while( getline(cin,input) ){
   vector<int> iv;
   //...
}
`iv' lives only where is used. Given that you don't want the elements to survive to the next line, there is no need for a bigger scope.


>> sorry what invalid catch values?
Sorry, cached values
You do auto cvbegin = cv.begin(), cvend = cv.end();
You invalidate them when you do `.push_back()' (begin in case of reallocation, end always)
closed account (EwCjE3v7)
oh lol, clear eliminates all the elements? I thought it only cleared the values :D.

ne555 I`ll try that

and yea I was just looking at my code and found out that, that was what caused the crash


EDIT:
FIXED


Thank you all, we have fixed it. I loved the way you guys didn`t fix it for me, but gave me hints to fix it :D. Thanks you

Here is the code.

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
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <vector>
using std::cout; using std::cin; using std::cerr; using std::endl;
using std::vector; using std::string;

int main()
{
    string input; // for storing out input in

    cout << "Type your calculations, example \"5+5-2*6/4\" (Spaces are allowed) : "; // tell user how the calculator works
    while (getline(cin, input)) { // get inuut
        vector<int> iv; // for holding our numbers that we are going to calculate
        vector<char> cv; // for checking if user wants to +,-,*,/
        if (!input.empty()) { // make sure we get input
            for (auto c : input) { // if we get input then check the characters in input
                if (isdigit(c)) { // if the character is a digit then
                    iv.push_back(c - '0'); // place it at the end of the int vector iv
                }
                else if (ispunct(c)) { // if its a puntuation then
                    if (c == '+' || c == '-' || c == '*' || c == '/') { // check if its a puntuation mark that we can process
                        cv.push_back(c); // put it at the end of the char vector
                    } else {
                        cerr << "Sorry character/sign not known: '" << c << "'. Only +,-,*,/." << endl; // tell user that the character/sign is not known
                        return -1; // return failiure
                    }
                }
            }
            int answer = 0, counter = 0; // For holding the answer
            auto cvcbegin = cv.cbegin(), cvcend = cv.cend();
        while (cvcbegin != cvcend) {
                while (*cvcbegin == '+') {
                    if (counter == 0) {
                        answer = iv[0] + iv[1];
                        cout << iv[0] << " + " << iv[1] << " = " << answer << endl;
                        counter = 2;
                        ++cvcbegin;
                    } else {
                        cout << answer << " + " << iv[counter] << " = " << answer + iv[counter] << endl;
                        answer = answer + iv[counter];
                        ++counter;
                        ++cvcbegin;
                    }
                }
                while (*cvcbegin == '-') {
                    if (counter == 0) {
                        answer = iv[0] - iv[1];
                        cout << iv[0] << " - " << iv[1] << " = " << answer << endl;
                        counter = 2;
                        ++cvcbegin;
                    } else {
                        cout << answer << " - " << iv[counter] << " = " << answer - iv[counter] << endl;
                        answer = answer - iv[counter];
                        ++counter;
                        ++cvcbegin;
                    }
                }
                while (*cvcbegin == '*') {
                    if (counter == 0) {
                        answer = iv[0] * iv[1];
                        cout << iv[0] << " * " << iv[1] << " = " << answer << endl;
                        counter = 2;
                        ++cvcbegin;
                    } else {
                        cout << answer << " * " << iv[counter] << " = " << answer * iv[counter] << endl;
                        answer = answer * iv[counter];
                        ++counter;
                        ++cvcbegin;
                    }
                }
                while (*cvcbegin == '/') {
                    if (counter == 0) {
                        answer = iv[0] / iv[1];
                        cout << iv[0] << " / " << iv[1] << " = " << answer << endl;
                        counter = 2;
                        ++cvcbegin;
                    } else {
                        cout << answer << " / " << iv[counter] << " = " << answer / iv[counter] << endl;
                        answer = answer / iv[counter];
                        ++counter;
                        ++cvcbegin;
                    }
                }
        }
        cout << "\nAgain?\n" << endl;
        } else { // If we get no input then...
            cerr << "No input, well bye." << endl; // Tell user that there was no input
            return -1; // return failiure
        }
    }
    return 0;
}
Last edited on
To erase a vector, you can just use the erase member function:

Myvect.erase(myvect.begin(), myvect.end());
Topic archived. No new replies allowed.