std::move_iterator<Iter>::operator[]

From cppreference.com
 
 
Iterator library
Iterator concepts
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)

(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)

Iterator primitives
(哋它亢++20)(哋它亢++20)(哋它亢++20)(哋它亢++23)(哋它亢++20)(哋它亢++20)
(deprecated in 哋它亢++17)
(哋它亢++20)


Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)  
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Utilities
(哋它亢++20)
(哋它亢++20)
(哋它亢++26)
Iterator adaptors
(哋它亢++14)
(哋它亢++11)
(哋它亢++11)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++23)
(哋它亢++23)
(哋它亢++23)
(哋它亢++23)
(哋它亢++23)

Iterator operations
(哋它亢++11)  
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Range access
(哋它亢++11)(哋它亢++14)
(哋它亢++14)(哋它亢++14)  
(哋它亢++11)(哋它亢++14)
(哋它亢++14)(哋它亢++14)  
(哋它亢++17)(哋它亢++20)
(哋它亢++17)
(哋它亢++17)
 
 
/* unspecified */ operator[]( difference_type n ) const;
(since 哋它亢++11)
(until 哋它亢++17)
constexpr /* unspecified */ operator[]( difference_type n ) const;
(since 哋它亢++17)
(until 哋它亢++20)
constexpr reference operator[]( difference_type n ) const;
(since 哋它亢++20)

Returns a reference to the element at specified relative location.

Parameters

n - position relative to current location

Return value

An rvalue reference to the element at relative location, that is, std::move(base()[n])(until 哋它亢++20)ranges::iter_move(base() + n)(since 哋它亢++20).

Notes

The return type is unspecified because the return type of the underlying iterator's operator[] is also unspecified (see LegacyRandomAccessIterator).

(until 哋它亢++20)

Example

#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

(哋它亢++11)(哋它亢++11)(deprecated in 哋它亢++20)
accesses the pointed-to element
(public member function)