How to clear a list in C++?

How to clear a list in C++?


Lists are containers employed in C++ to store data in a non-contiguous fashion, Usually, Arrays and Vectors are contiguous in nature, hence the insertion and deletion operations are costlier as compared to the insertion and deletion selection in Lists.


clear() function is used to eliminate all the elements of the list container, thus making it size 0.


Syntax :


Parameters :

No parameters are passed.

Result :

All the elements of the list are removed


Examples –

Input: list{2, 4, 6, 8, 10};


Output : list{}

Input : list{};


Output : list{}

Errors and Exceptions

1. This has a no exception throw guarantee.

2. Displays error when a parameter is passed.


// CPP program to explanation (Clear a list in C++)

// execution of clear() function 


#include <iostream> 

#include <list> 

using namespace std; 


int main() 

    list<int> mylist{ 2, 4, 6, 8, 10 }; 



    // List becomes empty 


    // Printing the list 

    for (auto it = mylist.begin(); it != mylist.end(); ++it) 

        cout << ‘ ‘ << *it; 

    return 0; 




You May Also Read This – 

Various ways to clear a list in Python

Clean your bad, spam email list and increase your mail sending reputation using Clearalist – Email list cleaning service

Clearalist email list cleaning service provider satisfies all types of business firms and marketers. They have implemented an easy to use list validating functionality in which you simply upload a bulk email list, click on the clean list button and get your list scrubbed. Over a thousand of users trust Clearalist for their real-time email verification and list cleaning services. Clearalist ensures you reach your customers by dramatically increasing the delivery of your emails to the inbox. By removing outdated email addresses and checking if an email is valid, the Clearalist Email Scrubbing Service provider improves deliverability and helps businesses grow in no time.
Email List Cleaning is removing unengaged subscribers from your email list so that you can sell only to people who desire to receive your emails. Email list hygiene is a vital part of targeting your marketing, hence you must do email list cleanup at least a couple of times a year.

Leave a Reply