首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::size

Defined in header <iterator>

template < class C > constexpr auto size( const C& c ) -> decltype(c.size());

(1)

(since C++17)

template < class T, size_t N > constexpr size_t size( const T (&array)N ) noexcept;

(2)

(since C++17)

返回给定容器的大小。c或阵列array...

1%29c.size()...

2%29N...

参数

c

-

a container with a size method

array

-

an array of arbitrary type

返回值

…的大小carray...

例外

2%29

noexcept规格:

noexcept

注记

除了被纳入<iterator>,,,std::size如果包括下列任何一个标头,则保证可用:<array>,,,<deque>,,,<forward_list>,,,<list>,,,<map>,,,<regex>,,,<set>,,,<string>,,,<unordered_map>,,,<unordered_set>,和<vector>...

可能的实施

第一版

*。

模板<class C>自动大小%28 const C&c%29->解密类型%28 c.大小%28%29%29{返回c.大小%28%29;}

第二版

模板<类T,STD::size[医]tN>std::大小[医]T大小%28 const T%28和数组%29n%29 no({返回N;}除外)

二次

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <iterator>
 
int main() 
{
    std::vector<int> v = { 3, 1, 4 };
    std::cout << std::size(v) << '\n'; 
 
    int a[] = { -5, 10, 15 };
    std::cout << std::size(a) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
3
3

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券