Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::size,std::ssize

      From cppreference.com
      <cpp‎ |iterator
       
       
      Iterator library
      Iterator concepts
      Iterator primitives
      Algorithm concepts and utilities
      Indirect callable concepts
      Common algorithm requirements
      (C++20)
      (C++20)
      (C++20)
      Utilities
      (C++20)
      Iterator adaptors
      Range access
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++11)(C++14)
      (C++14)(C++14)  
      sizessize
      (C++17)(C++20)
      (C++17)
      (C++17)
       
      Defined in header<array>
      Defined in header<deque>
      Defined in header<flat_map>
      Defined in header<flat_set>
      Defined in header<forward_list>
      Defined in header<inplace_vector>
      Defined in header<iterator>
      Defined in header<list>
      Defined in header<map>
      Defined in header<regex>
      Defined in header<set>
      Defined in header<span>
      Defined in header<string>
      Defined in header<string_view>
      Defined in header<unordered_map>
      Defined in header<unordered_set>
      Defined in header<vector>
      template<class C>
      constexprauto size(const C& c)-> decltype(c.size());
      (1)(since C++17)
      template<class C>

      constexprauto ssize(const C& c)
         ->std::common_type_t<std::ptrdiff_t,

                               std::make_signed_t<decltype(c.size())>>;
      (2)(since C++20)
      template<class T,std::size_t N>
      constexprstd::size_t size(const T(&array)[N])noexcept;
      (3)(since C++17)
      template<class T,std::ptrdiff_t N>
      constexprstd::ptrdiff_t ssize(const T(&array)[N])noexcept;
      (4)(since C++20)

      Returns the size of the given range.

      1,2) Returnsc.size(), converted to the return type if necessary.
      3,4) ReturnsN.

      Contents

      [edit]Parameters

      c - a container or view with asize member function
      array - an array of arbitrary type

      [edit]Return value

      1)c.size()
      2)static_cast<std::common_type_t<std::ptrdiff_t,
                                     std::make_signed_t<decltype(c.size())>>>(c.size())
      3,4)N

      [edit]Exceptions

      1,2) May throw implementation-defined exceptions.

      [edit]Overloads

      Custom overloads ofsize may be provided for classes and enumerations that do not expose a suitablesize() member function, yet can be detected.

      Overloads ofsize found byargument-dependent lookup can be used to customize the behavior ofstd::ranges::size,std::ranges::ssize, andstd::ranges::empty.

      (since C++20)

      [edit]Possible implementation

      size (1)
      template<class C>constexprauto size(const C& c)-> decltype(c.size()){return c.size();}
      ssize (2)
      template<class C>constexprauto ssize(const C& c)->std::common_type_t<std::ptrdiff_t,std::make_signed_t<decltype(c.size())>>{using R=std::common_type_t<std::ptrdiff_t,std::make_signed_t<decltype(c.size())>>;returnstatic_cast<R>(c.size());}
      size (3)
      template<class T,std::size_t N>constexprstd::size_t size(const T(&array)[N])noexcept{return N;}
      ssize (4)
      template<class T,std::ptrdiff_t N>constexprstd::ptrdiff_t ssize(const T(&array)[N])noexcept{return N;}

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_nonmember_container_access201411L(C++17)std::size(),std::data andstd::empty
      __cpp_lib_ssize201902L(C++20)std::ssize()(2,4) and unsignedstd::span::size()

      [edit]Example

      Run this code
      #include <cassert>#include <cstring>#include <iostream>#include <vector> int main(){// Works with containersstd::vector<int> v{3,1,4};assert(std::size(v)==3); // And works with built-in arrays tooint a[]{-5,10,15};// Returns the number of elements (not bytes) as opposed to sizeofassert(std::size(a)==3);std::cout<<"size of a[]: "<< sizeof a<<'\n';// 12, if sizeof(int) == 4 // Provides a safe way (compared to sizeof) of getting string buffer sizeconstchar str[]="12345";// These are fine and give the correct resultassert(std::size(str)==6);assert(sizeof(str)==6); // But use of sizeof here is a common source of bugsconstchar* str_decayed="12345";// std::cout << std::size(str_decayed) << '\n'; // Usefully fails to compilestd::cout<< sizeof(str_decayed)<<'\n';// Prints the size of the pointer! // Since C++20 the signed size (std::ssize) is availableauto i= std::ssize(v);for(--i; i!=-1;--i)std::cout<< v[i]<<(i?' ':'\n');assert(i==-1); // Note that the string literal includes the ending null character, which// will be part of the constructed characters array. This makes std::size// behave differently from std::strlen and std::string::size:constexprchar symbols[]="0123456789";     static_assert(std::size(symbols)==11);    static_assert(std::string(symbols).size()==10);assert(std::strlen(symbols)==10);}

      Possible output:

      size of a[]: 1284 1 3

      [edit]See also

      signed integer type returned when subtracting two pointers
      (typedef)[edit]
      unsigned integer type returned by thesizeof operator
      (typedef)[edit]
      returns an integer equal to the size of a range
      (customization point object)[edit]
      returns a signed integer equal to the size of a range
      (customization point object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/size&oldid=177485"

      [8]ページ先頭

      ©2009-2025 Movatter.jp