C++ set end
The C++ set end function is utilized to retrieve an iterator that points to the element following the last element in the set.
Note:- This is a placeholder. No element exists in this location and attempting to access is undefined behavior.
Syntax
iterator end(); //until C++ 11
const_iterator end() const; //until C++ 11
iterator end() noexcept; //since C++ 11
const_iterator end() const noexcept; //since C++ 11
Parameter
Return value
It returns an iterator positioned after the final element of the set.
Complexity
Constant.
Iterator validity
No changes.
Data Races
Simultaneously retrieving the elements of a set is considered secure.
The container is accessed without allowing modifications in either the mutable or immutable versions.
Exception Safety
This member function never throws exception.
Example 1
Let's see the simple example for end function:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<string> myset = {"Java", "C++", "SQL"};
// show content:
for (set<string>::iterator it=myset.begin(); it!=myset.end(); ++it)
cout << *it<< '\n';
return 0;
}
Output:
C++
Java
SQL
In the aforementioned example, the end method is employed to retrieve an iterator indicating the element following the final element in the myset set.
Example 2
Let's examine a basic illustration of iterating through a set utilizing a for-each loop:
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> c;
c.insert(5);
c.insert(2);
c.insert(4);
c.insert(1);
c.insert(0);
c.insert(0);
c.insert(9);
set<int>::iterator i = c.begin();
while (i != c.end())
cout << *i++ << " ";
cout << endl;
}
Output:
0 1 2 4 5 9
Example 3
Let's examine a basic illustration of iterating through a set utilizing a while loop:
#include <iostream>
#include <set>
#include <string>
int main()
{
using namespace std;
set<string> myset = { "Nikita","Deep","Priya","Suman","Aman" };
cout<<"Elements of myset are: "<<endl;
set<string>::const_iterator it; // declare an iterator
it = myset.begin(); // assign it to the start of the set
while (it != myset.end()) // while it hasn't reach the end
{
cout << *it << "\n";
// print the value of the element icpp tutorials to
++it; // and iterate to the next element
}
cout << endl;
}
Output:
Elements of myset are:
Aman
Deep
Nikita
Priya
Suman
In the previous example, the end method is employed to retrieve an iterator indicating the element following the final one in the myset set.
Example 4
Let's see a simple example:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
int val;
set<int> c = {10,20,30,40,50};
cout<<"Enter value to find: ";
cin>>val;
auto result = c.find(val);
//find until end of the set elements
if (result != c.end()) {
cout << "Element found: "<< *result;
cout << endl;
} else {
cout << "Element not found." << endl;
}
return 0;
}
Output:
Enter value to find: 60
Element not found.
Enter value to find: 30
Element found: 30
The end function is employed to retrieve an iterator that points to the element following the last one in the set named myset.