Spec-Zone.ru › C++

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

/* unspecified */ operator[]( difference_type n ) const;
(с C++11)
(до C++17)
constexpr /* unspecified */ operator[]( difference_type n ) const;
(с C++17)
(до C++20)
constexpr reference operator[]( difference_type n ) const;
(с C++20)

Возвращает ссылку на элемент в указанном относительном расположении.

Параметры

n - позиция относительно текущего местоположения

Значение результата

Ссылка на rvalue на элемент в относительном расположении, то есть std::move(base()[n])(до C++20)ranges::iter_move(base() + n)(с C++20).

Примечания

Тип результата не определён, так как тип возвращаемого значения оператора [] базового итератора также не определён (см. LegacyRandomAccessIterator).

(до C++20)

Пример

#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
}

Возможный вывод:

1) p: "alpha" "beta" "gamma" "delta"
2) p: "" "" "" ""
3) q: "alpha" "beta" "gamma" "delta"

См. также

operator*operator->
(C++11)(C++11)(устарело в C++20)
доступ к элементу, на который указывает итератор
(публичный метод)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/iterator/move_iterator/operator_at

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API