YouTip LogoYouTip

Cpp Libs List Clear

# C++ clear Function | # (#) * (javascript:void(0);) * (javascript:void(0);) * (javascript:void(0);) * (javascript:void(0)) C++ Tutorial [C++ Tutorial](#)[C++ Introduction](#)[C++ Environment Setup](#)[C++ Basic Syntax](#)[C++ Comments](#)[C++ Data Types](#)[C++ Variable Types](#)[C++ Variable Scope](#)[C++ Constants](#)[C++ Modifier Types](#)[C++ Storage Classes](#)[C++ Operators](#)[C++ Loops](#)[C++ Decision Making](#)[C++ Functions](#)[C++ Numbers](#)[C++ Arrays](#)[C++ Strings](#)[C++ Pointers](#)[C++ References](#)[C++ Date & Time](#)[C++ Basic Input/Output](#)[C++ Structures](#)[C++ vector Container](#)[C++ Data Structures](#) ## C++ Object-Oriented Programming [C++ Classes & Objects](#)[C++ Inheritance](#)[C++ Operator Overloading and Function Overloading](#)[C++ Polymorphism](#)[C++ Data Abstraction](#)[C++ Data Encapsulation](#)[C++ Interfaces (Abstract Classes)](#)") ## C++ Advanced Tutorials [C++ Files and Streams](#)[C++ Exception Handling](#)[C++ Dynamic Memory](#)[C++ Namespaces](#)[C++ Templates](#)[C++ Preprocessor](#)[C++ Signal Handling](#)[C++ Multithreading](#)[C++ Web Programming](#) ## C++ Resources Library [C++ STL Tutorial](#)[C++ Importing Standard Libraries](#)[C++ Standard Library](#)[C++ Useful Resources](#)[C++ Examples](#)[C++ Quiz](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ``](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ](#)[C++ ```cpp #include #include int main() { std::list mylist = {1, 2, 3, 4, 5}; std::cout << "Before clear: "; for (const auto& elem : mylist) { std::cout << elem << " "; } std::cout << std::endl; mylist.clear(); std::cout << "After clear: "; for (const auto& elem : mylist) { std::cout << elem << " "; } std::cout << std::endl; return 0; } The `clear()` function in C++ `` container removes all elements from the list, leaving it empty. It does not change the capacity of the container; it simply erases all elements. ### Syntax ```cpp void clear(); ### Example In the example above, we first create a list with five integers. Then we print its contents before calling `clear()`. After clearing the list, we print its contents again, which will show nothing since all elements have been removed. ### Return Value The `clear()` function returns `void`. ### Time Complexity The time complexity of `clear()` is O(n), where n is the number of elements in the list. ### Related Functions : Checks if the list is empty. : Returns the number of elements in the list. : Removes specific elements from the list. ### See Also
← Cpp Libs List BackCpp Libs List Insert β†’