Why cant i pop back in the vector

Sep 3, 2012 at 9:52pm
I keep getting one error when i type in pop_back and it wont go away. The error is hard to read.

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

void ques()
{
    string str;
    int input;
    int rem;


    vector<string> strVect;

    cout << "how many Vector items will you have?" << endl;
    cin >> input;

    cin.ignore(80, '\n');

    for(int i = 0; i < input; i++)
    {
        cout << "Enter a name" << endl;
        getline(cin, str);
        strVect.push_back(str);
        cout << "\n";
        cout << "Would you like to remove any items?" << endl;
        cin >> rem;
        strVect.pop_back(str);
        cin.sync();
    }

    for(int i = 0; i < input; i++)
    {
        cout << strVect[i] << endl;
    }
}



||=== Containers, Debug ===|
C:\Users\Chay Hawk\Desktop\Containers\main.cpp||In function 'void ques()':|
C:\Users\Chay Hawk\Desktop\Containers\main.cpp|90|error: no matching function for call to 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::pop_back(std::string&)'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_vector.h|764|note: candidates are: void std::vector<_Tp, _Alloc>::pop_back() [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]|
||=== Build finished: 1 errors, 0 warnings ===|
Sep 3, 2012 at 9:56pm
strVect.pop_back();

The member function pop_back() takes no arguments. It just removes the last item pushed onto the vector.
Last edited on Sep 3, 2012 at 9:58pm
Sep 3, 2012 at 10:02pm
Line 24: vector::pop_back takes no arguments. It only removes the last element in the vector (in this case the last string).
strVect.pop_back();
Last edited on Sep 3, 2012 at 10:02pm
Sep 3, 2012 at 10:03pm
Oh, is there any way to remove certain elements? like if the user types in 5 then it will get rid of the first five or something?
Last edited on Sep 3, 2012 at 10:03pm
Sep 3, 2012 at 10:06pm
It'd just be a matter of looping pop_back 5 times..

If u need something to adjust the first elements too then maybe look into 'deque' instead of a 'vector' approach.
Last edited on Sep 3, 2012 at 10:10pm
Sep 4, 2012 at 5:30am
pop_back removes the last element of a vector...

if you used pop_back to do the following "like if the user types in 5 then it will get rid of the first five or something?" looping it 5 times will remove the last 5 elements not the first.

You need to use the erase function...
(Psudo)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::vector<int> intVector;

intVectior.push_back(1);
intVectior.push_back(5);
intVectior.push_back(90);

//remove the first 2 values in the vector.
intVector.erase(intVector.begin(), intVector.begin()+1);

std::cout << intVector.at(0); //should be 90

//if you however looped the pop_back...

for (int i=0; i<2; i++)
  intVector.pop_back();

std::cout << intVector.at(0); //should be 1
Sep 4, 2012 at 5:35am
so if i used intVector.at() i could delete any element? i want the user to be able to delete elements from the vector. so i f they want to delete 5 then it will delete 5 only or something like that.
Sep 4, 2012 at 5:48am
intVector.at(int i) reads the information at an index, its kind of like using [] on an array (which you can do to a vector).

Erase has a couple of options in it, the example i gave above is where you remove between one point and another...

To remove a single point, use only a single argument as below (say the 5th element):

 
intVector.erase(intVector.begin()+5);


This removes the element 5 elements from the start.

Similarly, if you want to remove the 5th element from the end:

 
intVector.erase(intVector.end()-5);


if you can follow the example: http://www.cplusplus.com/reference/stl/vector/erase/
Last edited on Sep 4, 2012 at 5:56am
Sep 4, 2012 at 5:55am
So will it delete the last element then go back 4 more? i only want to delete a certain element, like lets say the 2nd to last element, but still want to keep the last element, will that work?
Sep 4, 2012 at 5:59am
Not quite, i made a typo...

It will remove only the 5th element from the end.

Say you have the numbers in a vector of:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

intVector.erase(intVector.end()-5) will only erase the element 5 elements from the end of the vector...

So in this case, the value of 5 will be removed to leave a vector of:
0, 1, 2, 3, 4, 6, 7, 8, 9
Sep 4, 2012 at 6:03am
If you are performing many insertions/deletions vector probably isn't the best container. vectors are good for random access but are slow for insertion/deletion anywhere but the end. Maybe a list or deque would be better.
Sep 4, 2012 at 6:11am
Ok, well i have this:

1
2
3
4
5
6
7
8
9
10
11

    cout << "Would you like to remove any items?" << endl;
    cin >> rem;

    strVect.erase(strVect.end()-rem);


    for(int i = 0; i < input; i++)
    {
        cout << strVect[i] << endl;
    }


and when i entered 5 choiced it removed the one choice but copied the other choice for the rest of the output like this, this is how it would look all outputted without removing anything:

cake
cookies
punch
banana
brownie

now this is how it looks when i type in 2 element:

cake
cookies
punch
brownie
brownie

why did it change banana to brownie instead of remove it?
Last edited on Sep 4, 2012 at 6:13am
Sep 4, 2012 at 7:46am
closed account (DSLq5Di1)
The item has been erased, what remains is garbage. For optimal performance, std::vector avoids resizing the internal array, unless explicitly requested.

You see the garbage because you are iterating over the vector using the old size, i < input instead of the vector size strVect.size().
Sep 4, 2012 at 9:19am
ah, i got it working now thanks.
Sep 4, 2012 at 9:23am
how would i get my vector to start erasing from the front? i tried this

strVect.erase(strVect.front()+rem);

But it didnt work.
Sep 4, 2012 at 9:27am
This will remove the 5 first elements in the vector.
strVect.erase(strVect.begin(), strVect.begin() + 5);

This will only work if the vector has 5 or more elements.
Last edited on Sep 4, 2012 at 9:27am
Sep 4, 2012 at 9:30am
I only want it to remove one element in the vector though, thats what my above code already does i just want to reverse it.
Sep 4, 2012 at 9:39am
In that case, to remove the fifth element.
strVect.erase(strVect.begin() + 5); (seriphis mentioned this earlier)
Sep 4, 2012 at 9:40am
closed account (o3hC5Di1)
I think you're wanting to use a std::deque rather than a vector.
It offers the benefit of random access as well as being accessible from both the front and the back of the container.

http://cplusplus.com/reference/stl/deque/

All the best,
NwN

Topic archived. No new replies allowed.