C++ :: erase

The list::erase() is a built-in function in C++ STL which is used to delete elements from a list container. This function can be used to remove a single element or a range of elements from the specified list container.



    int main(){
        // Creating a list
        std::list<int> demoList;

        // Add elements to the List

        // Creating iterator to point to first
        // element in the list
        std::list<int>::iterator itr = demoList.begin();
        // Deleting the first element

        // Printing elements of list after deleting first element
        std::cout << "\nList after deleting first element:";
        for (auto itr = demoList.begin(); itr != demoList.end(); itr++) {
            std::cout << *itr << " ";

        return 0;

Leave a Reply

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