Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/size"

From cppreference.com
(move example to a template)
Line 22: Line 22:
  
 
===Example===
 
===Example===
{{todo|reason=Move this example into a template, use {{tl|example template}}}}
+
{{example template | cpp/container/{{{1|}}}/example_size}}
{{example
+
| The following code uses {{tt|size}} to display the number of elements in a {{c|std::{{{1}}}<int>}}:
+
| code=
+
#include <{{{1}}}>
+
#include <iostream>
+
+
int main()
+
{
+
    {{cpp/container/if map|{{{1|}}}
+
|std::{{{1}}}<int, char> nums {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
+
|std::{{{1}}}<int> nums {1, 3, 5, 7};
+
}}
+
+
    std::cout << "nums contains " << nums.size() << " elements.\n";
+
}
+
| output=
+
nums contains 4 elements.
+
}}
+
  
 
===See also===
 
===See also===

Revision as of 12:07, 16 May 2013

size_type size() const;

Returns the number of elements in the container, i.e. std::distance(begin(), end()).

Contents

Parameters

(none)

Return value

The number of elements in the container

Exceptions

noexcept specification:  
noexcept
  

Complexity

Constant

Example

See also

Template:cpp/container/dcl list emptyTemplate:cpp/container/dcl list max size