3

Set end() function in C++

 8 months ago
source link: https://thispointer.com/set-end-function-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This tutorial will discuss about the set end() function in C++.

Table Of Contents

Advertisements

liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY5Mwp1NDMkMSZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow1BQyQyMwAyMxYyMwBOVyRyp3QyMwAyMxYyMwAkJTIjJTJGJTIjNSZcp0FjpD0jJaNxn3Y9JaJcPTZDNwx3NwY1NTM3NDYkNmQ3MmqDNmM2QwMkN0I1NDMjN0Q3QwY0MmImMDMlMmMlRDMjMmtlRDMlMmM1RwMjMmQ3RDqCNmMmMTMlMmMmNDM5MmYmNDM5N0Q3QwQmMmEmNwM2N0Q3QwUmNwQ0NmY4NmA2MmMmNDI3NwYkNTpmNTMjNUE1ODQ5NmU1OTMlMmx3NDqEN0I2MwYmNwt3MwZGNxQ2NTqEN0I3MTY0NwU3MmZCNmQ2RwpjN0Q3QwZGNxM2OTZFNmU3ODqEN0I1ODM0MmUmMmqEN0I1OTMmMmImNTqEN0I2NwMkN0Q3QwplNTE1NTRBNTU0OTQmMmt2NmU0NxM1QTU1NUE1ODRFMmA0OTQmMmt2NmRENTM0MTp2NDx0NDU1M0Q3RDqCNEMmMTMlMmUmMwMkN0RGRUZFJzRcYWyxPSZ1p2VlSXBBZGRlPTQ1Lwp3LwE3OC4lMmUzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR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MxZmZXQgZW5xLWZ1ozN0nW9hLWyhLWMyMxYzZzkiYXRTqGF0qXM9ZzFfp2UzZWyxp3A9nWykJaB4nWQ9MTt5OTtmNwI0NA==

In C++, the set container from the Standard Template Library (STL) provides a way to store unique elements in a sorted sequence. To traverse or iterate over these elements, set provides the end() function which is crucial for defining the limit of the set during iteration.

Syntax od set::end()

Copy to clipboard
iterator end() noexcept;
iterator end() noexcept;

Parameters: None.

Return Value:
– Iterator pointing just past the last element in the set.

The set class provides a function named end() which returns an iterator that refers to the “past-the-end” element of the set. The term “past-the-end” refers to a position just after the last element in the set, essentially marking the set’s end.

The iterator returned by the end() function is commonly used in tandem with the begin() function. The begin() function returns an iterator pointing to the set’s first element. We can increment this iterator until it matches the “past-the-end” iterator returned by the end() function. This allows us to iterate over all elements in the set.

Example of set::end()

Let’s look at an example that demonstrates the use of the iterator returned by the end() function.

Let’s see the complete example,

Copy to clipboard
#include <iostream>
#include <set>
int main()
std::set<int> numbers = {11, 22, 33, 44, 55};
// Iterate over the Set elements using iterators
// returned by begin() and end() functions
for(std::set<int>::iterator it = numbers.begin();
it != numbers.end();
it++)
std::cout << *it << " ";
std::cout<< std::endl;
return 0;
#include <iostream>
#include <set>

int main()
{
    std::set<int> numbers = {11, 22, 33, 44, 55};

    // Iterate over the Set elements using iterators
    // returned by begin() and end() functions
    for(std::set<int>::iterator it = numbers.begin();
        it != numbers.end();
        it++)
    {
        std::cout << *it << " ";
    }
    std::cout<< std::endl;

    return 0;
}

Output

Copy to clipboard
11 22 33 44 55

Scenarios for Exceptions and Undefined Behavior

  1. Dereferencing End Iterators: The iterators returned by end() function point to a position just after the last element in the set. Dereferencing such iterators leads to undefined behavior. Always ensure that you don’t attempt to access the value pointed to by an end iterator.

  2. Iterator Invalidation: Operations like erase() may invalidate iterators. Using an invalidated iterator, especially in conjunction with end(), can cause undefined behavior.

  3. Thread Safety: using end() function concurrently in multiple threads can introduce race conditions. If one thread modifies the set (like inserting or erasing elements) while another thread is accessing its end, it may lead to undefined behavior. Proper synchronization is needed when accessing the set concurrently from multiple threads.

Summary

Today, we learned about set end() function in C++.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK