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();
Printable View
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();
how about just pop()?
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.Quote:
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();
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.
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
Quote:
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
d'oh... i was thinking of a queueQuote:
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()];
I do that all the time. I know better, yet I still do it. That's it, i'm outta here.Quote:
Originally posted by Sang-drax
Code:vectorname[vectorname.size()-1]; //Or my program is buggier than I think