Sobes.tech
Middle

How to remove an element from the beginning in a vector if the order of elements is not important?.

sobes.tech AI

Answer from AI

Move the last element to the position of the removed element and reduce the size of the vector.

#include <vector>
#include <algorithm>

void remove_front_order_irrelevant(std::vector<int>& vec) {
    if (!vec.empty()) {
        std::swap(vec.front(), vec.back()); // Move the last element to the front
        vec.pop_back(); // Remove the last element (now the former first)
    }
}