C++ set cbegin
C++ set cbegin function is used to return a constant iterator pointing to the first element of the set container.
Syntax
const_iterator cbegin() const noexcept; //since C++ 11
A const_iterator is an iterator thacpp tutorials to constant content.
Parameter
Return value
It returns a const_iterator pointing to the first element of the set.
Complexity
Constant
Iterator validity
No changes.
Data Races
The container is accessed.
Concurrently accessing the elements of a set is safe.
Exception Safety
This member function never throws exception.
Example 1
Let's see the simple example for cbegin function:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<string> myset= {"Java", "C++","SQL"};
// show content:
for (auto it = myset.cbegin(); it != myset.cend(); ++it)
cout <<*it << '\n';
return 0;
}
Output:
C++
Java
SQL
In the above example, cbegin function is used to return a constant iterator pointing to the first element in the myset set.
Example 2
Let's see a simple example:
#include <set>
#include <iostream>
int main( )
{
using namespace std;
set <int> s1;
set <int>::iterator s1_Iter;
set <int>::const_iterator s1_cIter;
s1.insert( 1 );
s1.insert( 2 );
s1.insert( 3 );
s1_Iter = s1.begin( );
cout << "The first element of s1 is " << *s1_Iter << endl;
s1_Iter = s1.begin( );
s1.erase( s1_Iter );
// The following 2 lines would err because the iterator is const
// s1_cIter = s1.begin( );
// s1.erase( s1_cIter );
s1_cIter = s1.begin( );
cout << "The first element of s1 is now " << *s1_cIter << endl;
}
Output:
The first element of s1 is 1
The first element of s1 is now 2
Example 3
Let's see a simple example to iterate over the set using while loop:
#include <iostream>
#include <set>
#include <string>
int main()
{
using namespace std;
set<string> myset = {"Robin","Dolly", "John","Nikita"};
set<string>::const_iterator it; // declare an iterator
it = myset.cbegin(); // assign it to the start of the vector
while (it != myset.cend()) // while it hasn't reach the end
{
cout << *it<< "\n";
// print the value of the element icpp tutorials to
++it; // and iterate to the next element
}
cout << endl;
}
Output:
Dolly
John
Nikita
Robin
In the above example, cbegin function is used to return an iterator pointing to the first element in the myset set.
Example 4
Let's see another simple example:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
set<int> number = {400, 350, 465, 290, 410};
cout << "Increasing order: " << '\n';
cout<<"______________________\n";
set<int>::const_iterator cit;
for (cit=number.cbegin(); cit!=number.cend(); ++cit)
cout << *cit<< '\n';
auto low = number.cbegin();
auto high = number.rbegin();
cout << "\nSmallest Number is: "<< *low <<endl;
cout<< "Biggest Number is: "<<*high <<endl;
return 0;
}
Output:
Increasing order:
______________________
290
350
400
410
465
Smallest Number is: 290
Biggest Number is: 465
In the above example, cbegin function is used to return an iterator pointing to the first element in the myset set.