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

假如想象 提交于 2019-12-01 02:49:36

问题


It seems safe to cast the result of my vector's size() function to an unsigned int. How can I tell for sure, though? My documentation isn't clear about how size_type is defined.


回答1:


Do not assume the type of the container size (or anything else typed inside).

Today?

The best solution for now 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 a typedef could help make this better to read)

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

After C++0x?

When the compiler will be able to find the type of the variable, you'll be able to use the auto keyword. For example:

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
}

Edit: Question from JF

What if he needs to pass the size of the container to some existing code that uses, say, an unsigned int? – JF

This is a problem common to the use of the STL: You cannot do it without some work.

The first solution is to design the code to always use the STL type. For example:

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 */
}

The second is to make the conversion yourself, using either a static_cast, using a function that will assert if the value goes out of bounds of the destination type (sometimes, I see code using "char" because, "you know, the index will never go beyond 256" [I quote from memory]).

I believe this could be a full question in itself.




回答2:


According to the standard, you cannot be sure. The exact type depends on your machine. You can look at the definition in your compiler's header implementations, though.




回答3:


I can't imagine that it wouldn't be safe on a 32-bit system, but 64-bit could be a problem (since ints remain 32 bit). To be safe, why not just declare your variable to be vector<MyType>::size_type instead of unsigned int?




回答4:


The C++ standard only states that size_t is found in <cstddef>, which puts the identifiers in <stddef.h>. My copy of Harbison & Steele places the minimum and maximum values for size_t in <stdint.h>. That should give you a notion of how big your recipient variable needs to be for your platform.

Your best bet is to stick with integer types that are large enough to hold a pointer on your platform. In C99, that'd be intptr_t and uintptr_t, also officially located in <stdint.h>.




回答5:


As long as you're sure that an unsigned int on your system will be large enough to hold the number of items you'll have in the vector you should be safe ;-)




回答6:


It should always be safe to cast it to size_t. unsigned int isn't enough on most 64-bit systems, and even unsigned long isn't enough on Windows (which uses the LLP64 model instead of the LP64 model most Unix-like systems use).




回答7:


I'm not sure how well this will work because I'm just thinking off the top of my head, but a compile-time assertion (such as BOOST_STATIC_ASSERT() or see Ways to ASSERT expressions at build time in C) might help. Something like:

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


来源:https://stackoverflow.com/questions/226302/where-can-i-look-up-the-definition-of-size-type-for-vectors-in-the-c-stl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!