std::ranges::subrange<I,S,K>::prev
來自 cppreference.com
constexpr subrange prev( std::iter_difference_t<I> n = 1 ) const requires std::bidirectional_iterator<I>; |
(C++20 起) | |
返回 *this 的一個副本,其 begin_
減量(若 n 為負則增量)。實際的減量(或增量)操作由 advance()
執行。
等價於:auto tmp = *this;
tmp.advance(-n);
return tmp;。
目錄 |
[編輯] 引數
n | - | 迭代器減量的次數 |
[編輯] 返回值
如上所述。
[編輯] 注意
此函式與 advance()
的區別在於後者就地執行減量(或增量)操作。
[編輯] 示例
執行此程式碼
#include <iterator> #include <list> #include <print> #include <ranges> int main() { std::list list{1, 2, 3, 4, 5}; std::ranges::subrange sub{std::next(list.begin(), 2), std::prev(list.end(), 2)}; std::println("{} {} {}", sub, sub.prev(), sub.prev(2)); }
輸出
[3] [2, 3] [1, 2, 3]
[編輯] 參閱
獲取 `subrange` 的副本,其迭代器按給定距離前進 (public member function) | |
將迭代器按給定距離前進 (public member function) | |
(C++11) |
遞減迭代器 (函式模板) |
(C++20) |
按給定距離或到邊界遞減迭代器 (演算法函式物件) |