Przejdź do głównej zawartości

std::unordered_set end() method

// Non const version
iterator end() noexcept;

// Const version
const_iterator end() const noexcept;

// Const version
const_iterator end() const noexcept;

Returns an iterator

to the element past-the-end of the unordered_set. If the array is empty, the returned iterator will be equal to begin().

Attempting to dereference a past-the-end iterator is undefined behaviour

.

Parameters

(none)

Return value

Iterator to the first element.

Exceptions

(none)

Complexity

Constant - O(1).

Difference between end and cend

For a const container c, end and cend are the same - c.end() == c.cend()

For non-const container of type c they return different iterators:

#include <unordered_set>

int main()
{
std::unordered_set<int> unordered_set = {1, 2, 3, 4, 5};
auto it = unordered_set.begin(); // Type: std::unordered_set<int>::iterator
*std::prev(it) = 5; // ✔ Ok
}

Example

Main.cpp
#include <iostream>
#include <unordered_set>

struct Point { double x, y; };

int main() {
Point pts[3] = { {1, 0}, {2, 0}, {3, 0} };

//points is a set containing the addresses of points
std::unordered_set<Point *> points = { pts, pts + 1, pts + 2 };

//Change each y-coordinate of (i, 0) from 0 into i^2 and print the point
for(auto iter = points.begin(); iter != points.end(); ++iter){
(*iter)->y = ((*iter)->x) * ((*iter)->x); //iter is a pointer-to-Point*
std::cout << "(" << (*iter)->x << ", " << (*iter)->y << ") ";
}
std::cout << '\n';

//Now using the range-based for loop, we increase each y-coordinate by 10
for(Point * i : points) {
i->y += 10;
std::cout << "(" << i->x << ", " << i->y << ") ";
}
}
Output
(3, 9) (1, 1) (2, 4) 
(3, 19) (1, 11) (2, 14)
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::unordered_set end() method

// Non const version
iterator end() noexcept;

// Const version
const_iterator end() const noexcept;

// Const version
const_iterator end() const noexcept;

Returns an iterator

to the element past-the-end of the unordered_set. If the array is empty, the returned iterator will be equal to begin().

Attempting to dereference a past-the-end iterator is undefined behaviour

.

Parameters

(none)

Return value

Iterator to the first element.

Exceptions

(none)

Complexity

Constant - O(1).

Difference between end and cend

For a const container c, end and cend are the same - c.end() == c.cend()

For non-const container of type c they return different iterators:

#include <unordered_set>

int main()
{
std::unordered_set<int> unordered_set = {1, 2, 3, 4, 5};
auto it = unordered_set.begin(); // Type: std::unordered_set<int>::iterator
*std::prev(it) = 5; // ✔ Ok
}

Example

Main.cpp
#include <iostream>
#include <unordered_set>

struct Point { double x, y; };

int main() {
Point pts[3] = { {1, 0}, {2, 0}, {3, 0} };

//points is a set containing the addresses of points
std::unordered_set<Point *> points = { pts, pts + 1, pts + 2 };

//Change each y-coordinate of (i, 0) from 0 into i^2 and print the point
for(auto iter = points.begin(); iter != points.end(); ++iter){
(*iter)->y = ((*iter)->x) * ((*iter)->x); //iter is a pointer-to-Point*
std::cout << "(" << (*iter)->x << ", " << (*iter)->y << ") ";
}
std::cout << '\n';

//Now using the range-based for loop, we increase each y-coordinate by 10
for(Point * i : points) {
i->y += 10;
std::cout << "(" << i->x << ", " << i->y << ") ";
}
}
Output
(3, 9) (1, 1) (2, 4) 
(3, 19) (1, 11) (2, 14)
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.