C++ Set Library - size Function



Description

It returns the number of elements in the set container.

Declaration

Following are the ways in which std::set::size works in various C++ versions.

C++98

size_type size() const;

C++11

size_type size() const noexcept;;

Return value

It returns the number of elements in the set container.

Exceptions

It never throws exceptions.

Time complexity

Time complexity is contstant.

Example

The following example shows the usage of std::set::size.

#include <iostream>
#include <set>

int main () {
   std::set<int> myints;
   std::cout << "0. size: " << myints.size() << '\n';

   for (int i = 0; i < 5; ++i) myints.insert(i);
   std::cout << "1. size: " << myints.size() << '\n';

   myints.insert (200);
   std::cout << "2. size: " << myints.size() << '\n';

   myints.erase(10);
   std::cout << "3. size: " << myints.size() << '\n';

   return 0;
}

The above program will compile and execute properly.

0. size: 0
1. size: 5
2. size: 6
3. size: 6
set.htm
Advertisements