
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ vector::clear() Function
The C++ vector::clear() function is used to remove every element from a vector and is defined in the <vector> library. When the vector is empty, its size is 0. Vectors are useful data structures that function like dynamic one-dimensional arrays.The time complexity of the clear() function is linear.
Calling this function does not guarantee that a reallocation will take place or that the vector capacity will change. It invalidates any iterators, references, or pointers referring to the contained elements. Any past-the-end iterators is also revoked. The clear() function has linear complexity that scales with the size of the container.
Syntax
Following is the syntax for C++ vector::clear() Function −
void clear() noexcept;
Parameters
It doesn't contains any kind of parameters.
Example 1
Let's consider the following example, where we are going to use the clear() function and retrieving the output which print total elements in vector before using clear() and after using clear().
#include <iostream> #include <vector> using namespace std; int main(){ vector<int> tutorial = {12,23,34,45,56,67}; unsigned int vectorsize = tutorial.size(); cout << "Before clear() :" << vectorsize << endl; tutorial.clear(); vectorsize = tutorial.size(); cout << "After clear(): " << vectorsize << endl; cout << endl; return 0; }
Output
When we compile and run the above program, this will produce the following result −
Before clear() :6 After clear(): 0
Example 2
In the following example, we are going to use the clear() function and observing that the capacity of the vector remains same after the use of clear().
#include <iostream> #include <algorithm> #include <vector> int main(){ std::vector<int> tutorial{11,22,33,44,55}; auto print = [](const int& n) { std::cout << " " << n; }; std::cout << "Before clear:"; std::for_each(tutorial.begin(), tutorial.end(), print); std::cout << "\nSize=" << tutorial.size() << ", Capacity=" << tutorial.capacity() << '\n'; std::cout << "Clear\n"; tutorial.clear(); std::cout << "After clear:"; std::for_each(tutorial.begin(), tutorial.end(), print); std::cout << "\nSize=" << tutorial.size() << ", Capacity=" << tutorial.capacity() << '\n'; }
Output
On running the above program, it will produce the following result −
Before clear: 11 22 33 44 55 Size=5, Capacity=5 Clear After clear: Size=0, Capacity=5
Example 3
Considering the following example, which throws an error as we declared vector with constant that makes the clear() function to throw error.
#include <vector> #include <iostream> using namespace std; int main(){ const vector<char> x = {'W', 'E', 'L', 'C', '0', 'M','E'}; for (vector<char>::const_iterator it = x.begin(); it != x.end(); it++) cout << *it << ' '; cout << endl; x.clear(); for (vector<char>::const_iterator it = x.begin(); it != x.end(); it++) cout << *it << ' '; cout << endl; return 0; }
Output
On running the above program, it will produce the following result −
main.cpp:10:12: error: passing 'const std::vector<char>