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::begin - cpprefjp C++日本語リファレンス

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

最終更新日時: 2026年09月19日 21時31分51秒 (JST)
rotarymars が更新

履歴 編集

function template
<iterator>

std::begin(C++11)

namespace std {
  template <class C>
  auto
    begin(C& c)
      -> decltype(c.begin());       // (1) C++11
  template <class C>
  constexpr auto
    begin(C& c)
      -> decltype(c.begin());       // (1) C++17
  template <class C>
  constexpr auto
    begin(C& c)
      noexcept(noexcept(c.begin()))
      -> decltype(c.begin());       // (1) C++26

  template <class C>
  auto
    begin(const C& c)
      -> decltype(c.begin());         // (2) C++11

  template <class C>
  constexpr auto
    begin(const C& c)
      -> decltype(c.begin());         // (2) C++17
  template <class C>
  constexpr auto
    begin(const C& c)
      noexcept(noexcept(c.begin()))
      -> decltype(c.begin());         // (2) C++26

  template <class T, size_t N>
  T*
    begin(T (&array)[N]);             // (3) C++11
  template <class T, size_t N>
  constexpr T*
    begin(T (&array)[N]) noexcept;    // (3) C++14
}

概要

範囲から先頭要素へのイテレータを取得する。

この関数は、メンバ関数版のbegin()とちがい、組み込み配列に対しても使用できる。

  • (1) : 非constのコンテナの、先頭要素へのイテレータを取得する
  • (2) : constのコンテナの、先頭要素へのイテレータを取得する
  • (3) : 組み込み配列の、先頭要素へのポインタを取得する

戻り値

  • (1) : return c.begin();
  • (2) : return c.begin();
  • (3) : return array;

備考

  • <iterator>ヘッダを読み込む以外では、以下のヘッダが読み込まれている場合に、この関数を使用できる:
    • <array>
    • <deque>
    • <flat_map> (C++26)
    • <flat_set> (C++26)
    • <forward_list>
    • <hive> (C++26)
    • <inplace_vector> (C++26)
    • <list>
    • <map>
    • <regex>
    • <set>
    • <simd> (C++29)
    • <span> (C++20)
    • <string>
    • <string_view> (C++20)
    • <unordered_map>
    • <unordered_set>
    • <vector>
  • この関数は、範囲for文の実装に使用される。

例

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

void print(int x)
{
  std::cout << x << " ";
}

int main()
{
  // コンテナ
  {
    std::vector<int> v = {1, 2, 3};

    decltype(v)::iterator first = std::begin(v);
    decltype(v)::iterator last = std::end(v);

    std::for_each(first, last, print);
  }
  std::cout << std::endl;

  // 組み込み配列
  {
    int ar[] = {4, 5, 6};

    int* first = std::begin(ar);
    int* last = std::end(ar);

    std::for_each(first, last, print);
  }
}

出力

1 2 3 
4 5 6 

バージョン

言語

  • C++11

処理系

  • Clang: 9.0.1 ✅
  • GCC: 4.7.0 ✅
  • Visual C++: ??

参照

  • N2930 Range-Based For Loop Wording (Without Concepts)
  • boost::begin() - Boost Range Library
  • LWG2280 - begin/end for arrays should be constexpr and noexcept
  • P0031R0 A Proposal to Add Constexpr Modifiers to reverse_iterator, move_iterator, array and Range Access
  • P3016R6 Resolve inconsistencies in begin/end for valarray and braced-initializer-list
  • LWG Issue 4385. Including <simd> doesn't provide std::begin/end
    • この関数テンプレートが利用可能になるヘッダの一覧に<simd>が追加された。規格としてはC++29のワーキングドラフトへ適用されたが、<simd>が追加されたC++26で記載が漏れていたものであるため、C++26へ遡及して適用される
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.