Pop_Back() Return Value

pop_back() return value?

I think there is something related to the fact that copying an instance of the last object could throw an exception. When doing so, you're losing your object, since pop_back() did remove it from your container. Better with a few lines of code:

std::vector<AnyClass> holds = {...} ;
try {
const AnyClass result = holds.pop_back(); // The copy Ctor throw here!
} catch (...)
{
// Last value lost here.
}

pop_back() return value in std::list?

pop_back() returning a reference to the deleted element would not be desirable, as the reference would immediately be to an invalid element. If you'd like to copy construct the last element of a list to another then delete the element from the first list, you can use the below code. If you don't need to delete the element, just omit the call to std::list::pop_back().

std::list<int> firstList{1, 2, 3, 4, 5, 6};
std::list<int> secondList{1, 2, 3, 4};
secondList.push_back(firstList.back()); //the 6 from firstList has now been copied to secondList
firstList.pop_back(); //the 6 from firstList has now been removed

How to store a value obtained from a vector `pop_back()` in C++?

It may sound as pop as in returning a value. But it actually doesn't. The standard says that vector::pop_back should erase the last value, with no return value.

You can do:

auto val = a.back();
a.pop_back();

How we have a return value of pop() function in JavaScript?

None of the "Exceptions Thrown by T" cases pop up in JavaScript, because there simply is no assignment or construction operator invoked. JavaScript is a garbage-collected language, so everything is a handle to the actual object.
Calling pop just shrinks the array by one and returns the handle that was there without doing anything more.

If you want to reason about it in terms of C++, everything is a std::shared_ptr<T>, so pop just move-constructs that to its return value, which is exception-free.

When using vectors does pop_back remove values along with elements?

"values" and "elements" are the same thing. pop_back() removes the last value from the vector, if the vector is not empty.

vector is designed so that it is the programmer's responsibility to not access out of bounds of the vector. If a vector has 2 elements and you try to access the third element via any method except for at(), you cause undefined behaviour.

To get bounds checking, use values.at(0) instead of values[0], etc., and include a try...catch block to catch the resulting exception.

why after pop_back() operation it still prints the complete vector?

Everyone has focused on saying this is undefined behavior fix code, but question was why it works.

To understand why it works you must understand how vector works more or less.

  • Vector allocates some block of memory to store items.
  • size of this block is defined by capacity, which says how many items can be stored in vector, without allocating new block of memory
  • capacity is usually bigger then size of the vector
  • now when you remove items form vector, capacity and allocated block of memory doesn't change. This is an optimization.
  • when you remove item from back just destructor is called (for int it does nothing) and size of vector is reduced.
  • your value is not cleared just marked as out of vector size
  • so when you use operator[] it doesn't check if you exceed its size. It just returns value at specific adders
  • since pop_back just reduced size you value is still there

Note if you call shrink_to_fit after pop_back there is a great chance it will and with crash or you will not receive same value. Still this is undefined behavior and anything can happen.

Another way to see your code is bad is to use at which checks if index is in valid range.

How do I get an error from `pop_back()` if `size()` is 0?

Both libstdc++ and libc++ have a "debug mode" with assertions, that can be enabled using:

  • -D_GLIBCXX_DEBUG for libstdc++
  • -D_LIBCPP_DEBUG for libc++

Also -fsanitize=undefined appears to catch it, but the error message is much more cryptic.

error: void value not ignored as it ought to be

std::vector<T>::pop_back() returns void. You attempt to return it as an int. This is not allowed.



Related Topics



Leave a reply



Submit