C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ multiset cbegin()C++ multiset cbegin() function is used to return a constant iterator pointing to the first element of the multiset container. Syntaxconst_iterator cbegin() const noexcept; //since C++ 11 A const_iterator is an iterator that points to constant content. ParameterNone Return valueThe cbegin() funtion returns a const_iterator pointing to the first element of the multiset. ComplexityConstant Iterator validityNo changes. Data RacesThe container is accessed. Concurrently accessing the elements of a multiset container is safe. Exception SafetyThis member function never throws exception. Example 1Let's see the simple example for cbegin() function: #include <iostream> #include <set> using namespace std; int main () { multiset<string> mymultiset= {"Java", "C","SQL","C++" }; // show content for (auto it = mymultiset.cbegin(); it != mymultiset.cend(); ++it) cout <<*it << '\n'; return 0; } Output: C C++ Java SQL In the above example, cbegin() function is used to return a constant iterator pointing to the first element in the mymultiset multiset. Example 2Let's see a simple example: #include <set> #include <iostream> int main( ) { using namespace std; multiset <int> s1; multiset <int>::iterator s1_Iter; multiset <int>::const_iterator s1_cIter; s1.insert( 1 ); s1.insert( 2 ); s1.insert( 3 ); s1.insert( 2 ); 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 3Let's see a simple example to iterate over the multiset using while loop: #include <iostream> #include <set> #include <string> int main() { using namespace std; multiset<string> mymultiset = {"Robin","Dolly", "John","Nikita","Nikita"}; multiset<string>::const_iterator it; // declare an iterator it = mymultiset.cbegin(); // assign it to the start of the vector while (it != mymultiset.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 Nikita Robin In the above example, cbegin() function is used to return an iterator pointing to the first element in the mymultiset multiset. Example 4Let's see another simple example: #include <iostream> #include <string> #include <set> using namespace std; int main () { multiset<int> number = {400, 350, 465, 290, 410, 400}; cout << "Increasing order: " << '\n'; cout<<"______________________\n"; multiset<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 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 mymultiset multiset.
Next TopicC++ multiset
|