size of set c++
#include <iostream>
#include <set>
int main() {
std::set<int> mySet;
mySet.insert(10);
mySet.insert(20);
mySet.insert(30);
mySet.insert(40);
mySet.insert(50);
std::cout << "Size of set: " << mySet.size() << std::endl;
return 0;
}
Explanation:
#include <iostream>
: Includes the input-output stream header file, which allows input and output operations.#include <set>
: Includes the header file for the set container in C++.std::set<int> mySet;
: Declares a set namedmySet
that stores integers.
Inside the main()
function:
- mySet.insert(10);
, mySet.insert(20);
, mySet.insert(30);
, mySet.insert(40);
, mySet.insert(50);
: Inserts integers 10, 20, 30, 40, and 50 into the set mySet
.
- std::cout << "Size of set: " << mySet.size() << std::endl;
: Outputs the size of the set mySet
using mySet.size()
. This function returns the number of elements present in the set.
return 0;
: Indicates successful termination of themain()
function.