std::move_i...:operator[](3)
π std::move_iterator::operator[] β bestie, itβs indexing but make it move β¨
Okay so this is the std::move_i...:operator[](3) page from the C++ Standard Libary. Itβs literally all about std::move_iterator::operator[], and itβs giving βreturn a reference to the element at a specified relative locationβ energy. π«Ά
πΈ NAME πΈ
std::move_iterator::operator[] β yes, the page title repeats itself: std::move_iterator::operator[]. Iconic, honestly. π
β¨ Synopsis β¨
Hereβs the exact signature tea, with all the since/until eras kept intact:
/* unspecified */ operator[]( difference_type n ) const;
(since C++11)
(until C++17)
constexpr /* unspecified */ operator[]( difference_type n ) const;
(since C++17)
(until C++20)
constexpr reference operator[]( difference_type n ) const;
(since C++20)
π Parameters π
nβ position relative to current location.
π Return value π
An rvalue reference to the element at that relative location. Like, literally:
- until C++20:
std::move(base()[n]) - since C++20:
ranges::iter_move(base() + n)
π§ Notes π§
Real talk: the return type is unspecified because the return type of the underlying iteratorβs operator[] is also unspecified β see LegacyRandomAccessIterator. The (until C++20) label is part of this note, bestie, donβt @ me. π
π Example π
// Run this code
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <string>
#include <vector>
void print(auto rem, auto const& v)
{
for (std::cout << rem; auto const& e : v)
std::cout << std::quoted(e) << ' ';
std::cout << '\n';
}
int main()
{
std::vector<std::string> p{"alpha", "beta", "gamma", "delta"}, q;
print("1) p: ", p);
std::move_iterator it{p.begin()};
for (std::size_t t{}; t != p.size(); ++t)
q.emplace_back(it[t]);
print("2) p: ", p);
print("3) q: ", q);
std::list l{1, 2, 3};
std::move_iterator it2{l.begin()};
// it2[1] = 13; // compilation error: the underlying iterator
// does not model the random access iterator
// *it2 = 999; // compilation error: using rvalue as lvalue
}
π Possible output π
1) p: "alpha" "beta" "gamma" "delta"
2) p: "" "" "" ""
3) q: "alpha" "beta" "gamma" "delta"
π¦ See also π¦
The see-also table references operator* and operator-> β operator-> accesses the pointed-to element. It also carries these exact labels: (C++11), (public member function), and (C++11)(deprecated in C++20). Nothing dropped, bestie. π
Thatβs the whole page, bestie. Footer source/version/date energy: http://cppreference.com Β· 2024.06.10 Β· std::move_i...:operator[](3) β mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms