From cppreference.com
/* unspecified */operator[](difference_typen)const;(constexpr since C++17)
(until C++20)constexprreferenceoperator[](difference_typen)const;(since C++20)Returns a reference to the element at specified relative location.
Parameters
n - position relative to current location Return value
std::move(
[n])(until C++20)ranges::iter_move(
+n)(since C++20)
Notes
The return type is unspecified because the return type of the underlying iterator's operator[] is also unspecified (see
).
(until C++20)Example
Run this code
#include<cstddef>#include<iomanip>#include<iostream>#include<iterator>#include<list>#include<string>#include<vector>voidprint(autorem,constauto&v){for(std::cout<<rem;constauto&e:v)std::cout<<std::quoted(e)<<' ';std::cout<<'\n';}intmain(){std::vector<std::string>p{"alpha","beta","gamma","delta"},q;print("1) p: ",p);std::move_iteratorit{p.begin()};for(std::size_tt{};t!=p.size();++t)q.emplace_back(it[t]);print("2) p: ",p);print("3) q: ",q);std::listl{1,2,3};std::move_iteratorit2{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