std::move_iterator<Iter>::operator[]
来自cppreference.com
< cpp | iterator | move iterator
迭代器库
迭代器概念 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
迭代器原语 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
算法概念与工具 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
间接可调用概念 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
常用算法要求 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
工具 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
迭代器适配器 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
流迭代器 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
迭代器定制点 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++20)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++20)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
迭代器操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
范围访问 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
std::move_iterator
成员函数 | ||||
move_iterator::operator[]
|
||||
非成员函数 | ||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
/* 未指定 */
operator[]( difference_type n ) const;
|
(C++11 起) (C++17 前) |
|
constexpr /* 未指定 */ operator[]( difference_type n ) const;
|
(C++17 起) | |
返回在指定相对位置的引用。
参数
n | - | 相对于当前位置的位置 |
返回值
到在相对位置元素的右值引用,即 std::move(base()[n]) (C++20 前)ranges::iter_move(base() + n) (C++20 起)。
注解
不指定返回类型的原因是底层迭代器的 operator[] 也没有指定返回类型(见 老式随机访问迭代器 (LegacyRandomAccessIterator) )。
示例
运行此代码
#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 << 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 (size_t t{0U}; 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; // 编译错误:底层迭代器未实现随机访问迭代器 // *it2 = 999; // 编译错误:以右值为左值 }
可能的输出:
1) p: "alpha" "beta" "gamma" "delta" 2) p: "" "" "" "" 3) q: "alpha" "beta" "gamma" "delta"
参阅
(C++11)(C++11)(C++20
中弃用)
|
访问被指向的元素 (公开成员函数) |