GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::prev - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. iterator
  3. prev

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<iterator>

std::prev(C++11)

namespace std {
  template <class BidirectionalIterator>
  BidirectionalIterator prev(BidirectionalIterator x,
                             typename std::iterator_traits<BidirectionalIterator>::difference_type n = 1); // C++11

  template <class BidirectionalIterator>
  constexpr BidirectionalIterator prev(BidirectionalIterator x,
                             typename std::iterator_traits<BidirectionalIterator>::difference_type n = 1); // C++17
}

概要

n回後方に進めたイテレータを返す。

advance()と違い、引数として渡されたイテレータへの参照を書き換えるのではなく、n回後方に進んだイテレータのコピーを返す。

効果

advance(x, -n);
return x;

戻り値

引数として渡されたイテレータをn回後方に進めたイテレータのコピー

例

#include <iostream>
#include <iterator>
#include <vector>

int main()
{
  std::vector<int> v = {3, 1, 4, 5, 2};

  {
    decltype(v)::iterator it = std::prev(v.end()); // イテレータを1回逆に進める
    std::cout << *it << std::endl;
  }
  {
    decltype(v)::iterator it = std::prev(v.end(), 2); // イテレータを2回逆に進める
    std::cout << *it << std::endl;
  }
}

出力

2
5

バージョン

言語

  • C++11

処理系

  • Clang: 3.0 ✅
  • GCC: 4.4 ✅
  • Visual C++: ??

関連項目

名前 説明
next() n回前方に進めたイテレータを返す
advance() n回イテレータを進める

参照

  • boost::prior() - Boost Utility Library
  • N2246 2 of the least crazy ideas for the standard library in C++0x
  • LWG Issue 2353. std::next is over-constrained
  • P0031R0 A Proposal to Add Constexpr Modifiers to reverse_iterator, move_iterator, array and Range Access
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.