Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The primary time to use <code>size_type</code> is in a template. Although <code>std::vector&lt;T&gt;::size_type</code> is usually <code>size_t</code>, <code>some_other_container&lt;T&gt;::size_type</code> might be some other type instead<sup>1</sup>. One of the few things a user is allowed to add to the <code>std</code> namespace is a specialization of an existing template for some user defined type. Therefore, <code>std::vector&lt;T&gt;::size_type</code> for some oddball <code>T</code> <em>could</em> actually be some type other than <code>size_t</code>, even though the base template defined in the standard library probably always uses <code>size_t</code>.</p> <p>Therefore, if you want to use the correct type for a specific container inside a template that works with that container, you want to use <code>container::size_type</code> instead of just assuming <code>size_t</code>.</p> <p>Note, however, that generic code should rarely work directly with a container. Instead, it should typically work with iterators, so instead of <code>container&lt;T&gt;::size_type</code>, it would typically use something like <code>std::iterator_traits&lt;WhateverIterator&gt;::difference_type</code> instead.</p> <hr> <ol> <li>And for some specific <code>T</code>, <code>vector&lt;T&gt;::size_type</code> might be a different type as well--one of the few things you're allowed to put into the <code>std</code> namespace is a specialization of an existing class for a user-defined type, so for some <code>T</code>, <code>vector&lt;T&gt;</code> could use a completely different container than for most other types. This is typical for <code>vector&lt;bool&gt;</code>, but possible for other types as well.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload