std::move_iterator<Iter>::operator[]
来自 cppreference.com
< cpp | iterator | move iterator
/* 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) | |
返回对指定相对位置处的元素的引用。
内容 |
[编辑] 参数
n | - | 相对于当前位置的位置 |
[编辑] 返回值
对指定相对位置处的元素的右值引用,即 std::move(base()[n])(until C++20)ranges::iter_move(base() + n)(since C++20).
[编辑] 注释
返回值未指定,因为底层迭代器的 operator[] 的返回值也未指定(参见 LegacyRandomAccessIterator)。 |
(until 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"
[编辑] 另请参阅
(C++11)(C++11)(deprecated in C++20) |
访问指向的元素 (公共成员函数) |