Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/size"

From cppreference.com
< cpp‎ | iterator
(Created page with "{{cpp/title| size}} {{cpp/iterator/navbar}} {{dcl begin}} {{dcl header | iterator}} {{dcl | num=1 | since=c++17 | template < class C> constexpr auto size (const C& c) -> declt...")
 
Line 26: Line 26:
 
===Return value===
 
===Return value===
 
The size of {{tt|c}} or {{tt|array}}
 
The size of {{tt|c}} or {{tt|array}}
 +
 +
===Exceptions===
 +
@2@ {{noexcept}}
  
 
===Notes===
 
===Notes===
 
In addition to being included in {{tt|<iterator>}}, {{tt|std::size}} is guaranteed to become available if any of the following headers are included: {{tt|<array>}}, {{tt|<deque>}}, {{tt|<forward_list>}}, {{tt|<list>}}, {{tt|<map>}}, {{tt|<regex>}}, {{tt|<set>}}, {{tt|<string>}}, {{tt|<unordered_map>}}, {{tt|<unordered_set>}}, and {{tt|<vector>}}.
 
In addition to being included in {{tt|<iterator>}}, {{tt|std::size}} is guaranteed to become available if any of the following headers are included: {{tt|<array>}}, {{tt|<deque>}}, {{tt|<forward_list>}}, {{tt|<list>}}, {{tt|<map>}}, {{tt|<regex>}}, {{tt|<set>}}, {{tt|<string>}}, {{tt|<unordered_map>}}, {{tt|<unordered_set>}}, and {{tt|<vector>}}.
 +
 +
===Example===
 +
{{example
 +
|
 +
| code=#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';
 +
}
 +
| output=
 +
3
 +
3
 +
}}

Revision as of 09:20, 15 January 2015

 
 
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 <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)

Returns the size of the given container c or array array.

1) Returns c.size().
2) Returns N.

Contents

Parameters

c - a container with a size method
array - an array of arbitrary type

Return value

The size of c or array

Exceptions

2)
noexcept specification:  
noexcept
  

Notes

In addition to being included in <iterator>, std::size is guaranteed to become available if any of the following headers are included: <array>, <deque>, <forward_list>, <list>, <map>, <regex>, <set>, <string>, <unordered_map>, <unordered_set>, and <vector>.

Example

#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';
}

Output:

3
3