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.
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 7
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
CPP
// 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; } |
Output:
1 2 3 4 5
CPP
// 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; } |
Output:
a c g z
CPP
// 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; } |
Output:
Geeksforgeeks This is
Time Complexity: O(1)
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.
CPP
// 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; } |
Output:
1 2 3 4 5
CPP
// 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; } |
Output:
a c g z
CPP
// 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; } |
Output:
Geeksforgeeks This is
Time Complexity: O(1)