Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::array<T,N>::begin,std::array<T,N>::cbegin

      From cppreference.com
      <cpp‎ |container‎ |array

      [edit template]
       
       
       
       
      iterator begin()noexcept;
      (1)(since C++11)
      (constexpr since C++17)
      const_iterator begin()constnoexcept;
      (2)(since C++11)
      (constexpr since C++17)
      const_iterator cbegin()constnoexcept;
      (3)(since C++11)
      (constexpr since C++17)

      Returns an iterator to the first element of*this.

      If*this is empty, the returned iterator will be equal toend().

      range-begin-end.svg

      Contents

      [edit]Return value

      Iterator to the first element.

      [edit]Complexity

      Constant.

      [edit]Example

      Run this code
      #include <algorithm>#include <array>#include <iomanip>#include <iostream> int main(){std::cout<<std::boolalpha; std::array<int,0> empty;std::cout<<"1) "<<(empty.begin()== empty.end())<<' '// true<<(empty.cbegin()== empty.cend())<<'\n';// true// *(empty.begin()) = 42; // => undefined behavior at run-time  std::array<int,4> numbers{5,2,3,4};std::cout<<"2) "<<(numbers.begin()== numbers.end())<<' '// false<<(numbers.cbegin()== numbers.cend())<<'\n'// false<<"3) "<<*(numbers.begin())<<' '// 5<<*(numbers.cbegin())<<'\n';// 5 *numbers.begin()=1;std::cout<<"4) "<<*(numbers.begin())<<'\n';// 1// *(numbers.cbegin()) = 42; // compile-time error:// read-only variable is not assignable // print out all elementsstd::cout<<"5) ";std::for_each(numbers.cbegin(), numbers.cend(),[](int x){std::cout<< x<<' ';});std::cout<<'\n'; constexprstd::array constants{'A','B','C'};    static_assert(constants.begin()!= constants.end());// OK    static_assert(constants.cbegin()!= constants.cend());// OK    static_assert(*constants.begin()=='A');// OK    static_assert(*constants.cbegin()=='A');// OK// *constants.begin() = 'Z'; // compile-time error:// read-only variable is not assignable}

      Output:

      1) true true2) false false3) 5 54) 15) 1 2 3 4

      [edit]See also

      returns an iterator to the end
      (public member function)[edit]
      (C++11)(C++14)
      returns an iterator to the beginning of a container or array
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/array/begin&oldid=118941"

      [8]ページ先頭

      ©2009-2025 Movatter.jp