does this function pop out the top value in a vector? how do i pop out the top value and store it in another variable? it doesn't work when i do this:
x = stringvector.pop_back();
This is a discussion on pop_back() function in the vector class within the C++ Programming forums, part of the General Programming Boards category; does this function pop out the top value in a vector? how do i pop out the top value and ...
does this function pop out the top value in a vector? how do i pop out the top value and store it in another variable? it doesn't work when i do this:
x = stringvector.pop_back();
Vectors don't have a pop() function. Why not just use the [] operator? If you want the first element it'd be vectorname[0], if you want the last element it'd be vectorname[vectorname.size()];
Check the syntax of the pop_back function, its possible you have to pass a variable reference in rather than returning one.Originally posted by Vector Man
does this function pop out the top value in a vector? how do i pop out the top value and store it in another variable? it doesn't work when i do this:
x = stringvector.pop_back();
Couldn't think of anything interesting, cool or funny - sorry.
It supports and erase() method:
vector::size_type size;
size = svec.size();
svec.erase(size);
But you should also probably be using iterators to identify the type.
Last edited by Troll_King; 09-03-2002 at 04:23 AM.
Some function definitions:
vector's pop_back()'s return type is void. back() returns a reference to the last element, so just use that before you pop_back()Code:void pop_back(); reference back(); const_reference back() const; //The member function returns a reference to the last element of the controlled sequence, which must be non-empty.
here's a list of all the functions in vector:
just look at 'vector' in MSVC or check out this link at dinkumware:Code:vector allocator_type · assign · at · back · begin · capacity · clear · const_iterator · const_reference · const_reverse_iterator · difference_type · empty · end · erase · front · get_allocator · insert · iterator · max_size · operator[] · pop_back · push_back · rbegin · reference · rend · reserve · resize · reverse_iterator · size · size_type · swap · value_type · vector
http://dinkumware.com/manuals/reader...&h=vector.html
Originally posted by Eibro
Vectors don't have a pop() function. Why not just use the [] operator? If you want the first element it'd be vectorname[0], if you want the last element it'd be vectorname[vectorname.size()];Code:vectorname[vectorname.size()-1]; //Or my program is buggier than I think
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
d'oh... i was thinking of a queueOriginally posted by Eibro
Vectors don't have a pop() function. Why not just use the [] operator? If you want the first element it'd be vectorname[0], if you want the last element it'd be vectorname[vectorname.size()];
I do that all the time. I know better, yet I still do it. That's it, i'm outta here.Originally posted by Sang-drax
Code:vectorname[vectorname.size()-1]; //Or my program is buggier than I think