Skip to main content

std::unordered_set begin() method

// Non const version
iterator begin() noexcept;

// Const version
const_iterator begin() const noexcept;

// Const version
const_iterator cbegin() const noexcept;

Returns an iterator

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

Parameters

(none)

Return value

Iterator to the first element.

Exceptions

(none)

Complexity

Constant - O(1).

Difference between begin and cbegin

For a const container c, begin and cbegin are the same - c.begin() == c.cbegin()

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
*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 << ") ";
}
}
Possible 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 begin() method

// Non const version
iterator begin() noexcept;

// Const version
const_iterator begin() const noexcept;

// Const version
const_iterator cbegin() const noexcept;

Returns an iterator

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

Parameters

(none)

Return value

Iterator to the first element.

Exceptions

(none)

Complexity

Constant - O(1).

Difference between begin and cbegin

For a const container c, begin and cbegin are the same - c.begin() == c.cbegin()

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
*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 << ") ";
}
}
Possible 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.