TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ set cbegin() Function

C++ set cbegin() Function with tutorial for beginners and professionals with examples on constructor, destructor, operator=(), begin(), cbegin(), cend(), end(), crbegin(), empty(), max_size(), clear(), emplace_hint(), key_comp(), swap() etc.

<< Back to CPP

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 that points to constant content.

Parameter

None

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 it points 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.


Next TopicSet end() Function




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf