Remove Last Element from Vector in C++

Learn about how to remove last element from Vector in C++.

C++ provides a very useful data structure called vectors. These objects are similar to arrays in storing similar elements under a common name. However, they have a very useful additional feature. Vectors are dynamic in C++, which means that we can remove and add elements as we go.

To work with vectors we need to add the vector header file at the start of the program. There are a lot of ways to remove elements from vectors in C++. We will discuss how to remove last element from vector in C++.

Ways to Remove Last Element from Vector in C++

Let us now discuss the different methods to remove last element from vector in C++.

Using the vector::pop_back() function to remove last element from vector in C++

The pop_back() method is defined in the vector header file and is used to remove last element from vector in C++. We can use this function directly with the required vector.

For example,

Output:

7
1
2
5

In the above example, we created a vector v and removed the last element from this vector using the pop_back() function. We display the vector using a for loop afterward.

Using the vector::resize() Function to Remove Last Element from Vector in C++

The resize() function can be used to alter the size of any given vector. We can specify the required size within the function. When we reduce the size of a vector, all the extra elements get automatically removed.

To remove last element from vector in C++, we need to specify the new size after subtracting one from the total length of the vector. We can use the size() function to return the length of a vector.

See the code below.

Output:

7
1
2
5

Using the vector::rrase() Function to Remove Last Element from Vector in C++

The vector::erase() function is used commonly to remove an element in a given vector. We can specify the iterator that points to the position of the element which we want to delete.

To remove last element from vector in C++, we can use this function. We can use the end() function to return an iterator pointing to one past the last element. We can subtract one from this to point to the final element.

For example,

Output:

7
1
2
5

Conclusion

To wrap up, we discussed several methods to remove last element from vector in C++. The first method is very straightforward. We directly use the pop_back() function to remove last element from vector in C++.

We can also alter the size of the vector using the resize() function and reduce its length by one, which results in removing the last element. The erase() function also removes elements from a vector. We specify an iterator pointing to the last element to remove it using this function.

Was this post helpful?

Related Posts

  • 19 May

    Convert Vector to Array in C++

    Table of ContentsVectors and Arrays in C++Convert Vector to Arrays in C++Using the for loop to convert vector to array in C++Using the copy() function to convert vector to array in C++Using the transform() function to convert vector to array in C++Using the data() function to convert vector to array in C++Using the & operator […]

  • 18 March

    Write Vector to File in C++

    Table of ContentsWrite Vector to File in C++ using vector indicesWrite Vector to File in C++ Using IteratorWrite Vector to File in C++ Using Range-Based for LoopWrite Vector to File in C++ Using the for_each() FunctionWrite Vector to File in C++ Without Using a LoopConclusion This article discusses different methods to Write Vector to File […]

  • Remove element by value from vector in C++
    07 February

    Remove element by value in vector in C++

    Table of ContentsWhat are vectors in C++?Where can we remove an element from a vector?How to remove element by value in vector in C++?Using the std::find() function to remove element by value in vector in C++.Using the remove() function to remove element by value in vector in C++.Using the remove_copy() function to remove element by […]

  • Merge two vectors in C++
    11 June

    Merge two Vectors in C++

    Table of ContentsUsing merge() to merge two vectors in C++Using set_union() to merge two vectors in C++Using insert() to combine two vectors in C++Using move() & back_inserter to merge two vectors in C++Using copy() and back_inserter to concatenate two vectors in C++Using make_move_iterator() and insert() to merge two vectors in C++Conclusion vectors are like dynamic […]

  • Vector of Structs in C++
    18 May

    Vector of structs in C++

    Table of ContentsUsing Range ConstructorUsing Initializer listUsing custom constructorBonus ProgramConclusion In this article, we will see how to initialize and use vector of structs in C++. There are different ways to do it. This article will explain these methods clearly. Using Range Constructor We can utilize the range constructor for the initialization of vector of […]

  • Remove elements from vector in C++
    08 May

    How to remove element from a vector in C++

    How to remove elements from vector in C++

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.