pop_back just returns void, so I just can't use that?
erase is okay but it doesn't return anything....
Do I use a combination of both?
Thanks
Jack
Printable View
pop_back just returns void, so I just can't use that?
erase is okay but it doesn't return anything....
Do I use a combination of both?
Thanks
Jack
if "it" is an iterator referring to the to be removed element, "v" is the vector and "T" its value_type:
if T is only movable, just replace elem initializations above with a move.Code:// 1)
// requires T to be copyable
// will make v copy all elements after "it", order is preserved
T elem = *it;
v.erase(it);
// 2)
// requires T to be copyable
// no more copy, order is not preserved
using std::swap;
T elem = *it;
swap( *it, v.back() );
v.pop_back();
As you state, pop_back just deletes the last element and doesn't return anything. If you want the contents of the last element before you delete it then use back() before pop_back(). See http://www.cplusplus.com/reference/vector/vector/back/