std::move_iterator::operator[] - cppreference.com (original) (raw)
| /* unspecified */ operator[]( difference_type n ) const; | | (constexpr since C++17) (until C++20) | | ------------------------------------------------------------- | | ------------------------------------- | | constexpr reference operator[]( difference_type n ) const; | | (since C++20) |
Returns a reference to the element at specified relative location.
[edit] Parameters
| n | - | position relative to current location |
|---|
[edit] Return value
std::move(_[current](../move%5Fiterator.html#current "cpp/iterator/move iterator")_ [n])(until C++20)ranges::iter_move(_[current](../move%5Fiterator.html#current "cpp/iterator/move iterator")_ + n)(since C++20)
[edit] Notes
| The return type is unspecified because the return type of the underlying iterator's operator[] is also unspecified (see LegacyRandomAccessIterator). | (until C++20) |
|---|
[edit] Example
#include #include #include #include #include #include #include void print(auto rem, const auto& v) { for (std::cout << rem; const auto& 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:
- p: "alpha" "beta" "gamma" "delta"
- p: "" "" "" ""
- q: "alpha" "beta" "gamma" "delta"