set clear() in C++

erase(): This function is a build-in function in STL. This function erases all 

elements in a set. 

Note: this function only erases the elements and that if the elements themselves

are pointers, the pointed-to memory is not touched in any way. 

This function is available in the below file.

File: stl_set.h

Parameter: No parameters are required for this function.

Syntax:

st.clear()

For Example:

st = {1.2.3.4}

st.clear()  = > This erases all elements from the set . So, set becomes empty.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    set<intst;
    st.insert(4);
    st.insert(2);
    st.insert(3);
    st.insert(1);

    st.clear();

    cout << st.size() << "\n";

    return 0;
}


No comments:

Post a Comment