How to pop the first element of a vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};

    // Check if the vector is not empty
    if (!vec.empty()) {
        // Erase the first element
        vec.erase(vec.begin());
    }

    // Print the updated vector
    for (int i : vec) {
        std::cout << i << " ";
    }
}

   
/*
run:
   
2 3 4 5 
   
*/

 



answered May 1, 2025 by avibootz
...