multiset cbegin() and cend() function in C++ STL

  1. The multiset::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the first element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly.

    Syntax:

    constant_iterator multiset_name.cegin()
    

    Parameters: The function does not accept any parameters.

    Return value: The function returns a constant iterator pointing to the first element in the container.

    Below programs illustrate the multiset::cbegin() method.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate the
    // multiset::cbegin() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
      
        int arr[] = { 14, 10, 15, 11, 10 };
      
        // initializes the set from an array
        multiset<int> s(arr, arr + 5);
      
        // Prints the first element
        cout << "The first elements is: " << *(s.cbegin()) << endl;
      
        // prints all elements in set
        for (auto it = s.cbegin(); it != s.cend(); it++)
            cout << *it << " ";
      
        return 0;
    }

    chevron_right

    
    

    Output:



    The first elements is: 10
    10 10 11 14 15
    
  2. The multiset::cend() is a built-in function in C++ STL which returns a constant iterator pointing to the position past the last element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse in the set accordingly.

    Syntax:

    constant_iterator multiset_name.cend()
    

    Parameters: The function does not accept any parameters.

    Return value: The function returns a constant iterator pointing to the position past the last element in the container in the container.

    Below programs illustrate the multiset::cend() method.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate the
    // multiset::cend() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
      
        int arr[] = { 14, 10, 15, 11, 10, 15, 17, 17 };
      
        // initializes the set from an array
        multiset<int> s(arr, arr + 8);
      
        // prints all elements in set
        for (auto it = s.cbegin(); it != s.cend(); it++)
            cout << *it << " ";
      
        return 0;
    }

    chevron_right

    
    

    Output:

    10 10 11 14 15 15 17 17
    

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.