set::begin() and set::end() in C++ STL

Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.

set::begin()

begin() function is used to return an iterator pointing to the first element of the set container. begin() function returns a bidirectional iterator to the first element of the container.

Syntax :



setname.begin()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the first element.

Examples:

Input  : myset{1, 2, 3, 4, 5};
         myset.begin();
Output : returns an iterator to the element 1

Input  : myset{8, 7};
         myset.begin();
Output : returns an iterator to the element 8

Errors and Exceptions

1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.

filter_none

edit
close

play_arrow

link
brightness_4
code

// INTEGER SET EXAMPLE
// CPP program to illustrate
// Implementation of begin() function
#include <iostream>
#include <set>
using namespace std;
   
int main()
{
    // declaration of set container
    set<int> myset{1, 2, 3, 4, 5};
      
    // using begin() to print set
    for (auto it=myset.begin(); it != myset.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

1 2 3 4 5
filter_none

edit
close

play_arrow

link
brightness_4
code

// CHARACTER SET EXAMPLE
// CPP program to illustrate
// Implementation of begin() function
#include <iostream>
#include <set>
using namespace std;
   
int main()
{
    // declaration of set container
    set<char> myset{'a', 'c', 'g', 'z'};
      
    // using begin() to print set
    for (auto it=myset.begin(); it != myset.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

a c g z
filter_none

edit
close

play_arrow

link
brightness_4
code

// STRING SET EXAMPLE
// CPP program to illustrate
// Implementation of begin() function
#include <iostream>
#include <set>
#include <string>
using namespace std;
   
int main()
{
    // declaration of set container
    set<string> myset{"This", "is", "Geeksforgeeks"};
      
    // using begin() to print set
    for (auto it=myset.begin(); it != myset.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

Geeksforgeeks This is 

Time Complexity : O(1)

set::end()

It returns an iterator pointing to past the last element of the set container. Since it does not refer to a valid element, it cannot de-referenced end() function returns a bidirectional iterator.

Syntax :

setname.end()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to next of the last element.

Examples:


Input  : myset{1, 2, 3, 4, 5};
         myset.end();
Output : returns an iterator to next of 5

Errors and Exceptions

1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.

filter_none

edit
close

play_arrow

link
brightness_4
code

// INTEGER Example
// CPP program to illustrate
// Implementation of end() function
#include <iostream>
#include <set>
using namespace std;
   
int main()
{
    // declaration of set container
    set<int> myset{1, 2, 3, 4, 5};
      
    // using end() to print set
    for (auto it=myset.begin(); it != myset.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

1 2 3 4 5
filter_none

edit
close

play_arrow

link
brightness_4
code

// CHARACTER SET EXAMPLE
// CPP program to illustrate
// Implementation of begin() function
#include <iostream>
#include <set>
using namespace std;
   
int main()
{
    // declaration of set container
    set<char> myset{'a', 'c', 'g', 'z'};
      
    // using begin() to print set
    for (auto it=myset.begin(); it != myset.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

a c g z
filter_none

edit
close

play_arrow

link
brightness_4
code

// STRING SET EXAMPLE
// CPP program to illustrate
// Implementation of begin() function
#include <iostream>
#include <set>
#include <string>
using namespace std;
   
int main()
{
    // declaration of set container
    set<string> myset{"This", "is", "Geeksforgeeks"};
      
    // using begin() to print set
    for (auto it=myset.begin(); it != myset.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

Geeksforgeeks This is 

Time Complexity : O(1)



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.