Where can I find the definition of size_type for vectors in C ++ STL?

It seems safe to pass the result of my vector function size()to unsigned int. How can I say for sure? My documentation is not clear how it is defined size_type.

+5
source share
7 answers

Do not accept the size type of the container (or anything else printed inside).

Today?

The best solution at the moment is to use:

std::vector<T>::size_type

Where T is your type. For example:

std::vector<std::string>::size_type i ;
std::vector<int>::size_type j ;
std::vector<std::vector<double> >::size_type k ;

(Using typedef can help make it better to read)

The same goes for iterators and all other types inside STL containers.

After C ++ 0x?

, auto. :

void doSomething(const std::vector<double> & p_aData)
{
    std::vector<double>::size_type i = p_aData.size() ; // Old/Current way

    auto j = p_aData.size() ;    // New C++0x way, definition
    decltype(p_aData.size()) k;  // New C++0x way, declaration
}

: JF

, - , , , unsigned int? - JF

, STL: - .

- , STL. :

typedef std::vector<int>::size_type VIntSize ;

VIntSize getIndexOfSomeItem(const std::vector<int> p_aInt)
{
   return /* the found value, or some kind of std::npos */
}

, , static_cast, , , ( "char", " , 256" [ ]).

, .

+11

. . .

+4

, 32- , 64- ( ints 32-). , vector <MyType> :: size_type unsigned int?

+3

++ , size_t <cstddef> , < stddef.h > . Harbison and Steele size_t < stdint.h > . , .

, , . C99 intptr_t uintptr_t, < stdint.h > .

+1

, unsigned int , , , ; -)

0

size_t. unsigned int 64- , Windows unsigned long ( LLP64 LP64, Unix- ).

0

, , , (, BOOST_STATIC_ASSERT() . ASSERT C) . - :

BOOST_STATIC_ASSERT( sizeof( unsigned int) >= sizeof( size_type));
-1

All Articles