Is size_type and size_t the same?

as above?
Not necessarily.

I assume by size_type you mean the typedef inside most STL containers?

If so, then just because size_type was added to all the containers instead
of just using size_t means that the STL is reserving the right to make
size_type any type they like. (By default, in all implementations I'm aware
of size_type is a typedef of size_t).
1) in which libraries is size_t defined or present in? because I discovered that including either cstdlib or iostream would define size_t.
2) what datatype exactly is size_t typedef to?

3) in which libraries is size_type defined or present in?
4) since size_type is almost always typedef to size_t, I guess most of the time it would be of size_t type, am I right?

5) When and where are size_t and size_type used? because when I compare the result of an int and the result of member function length() of string class, it gives me a warning comparison of signed and unsigned types.

Thanks for the help
1.) Probably most things that use a size_type variable (since they typedef it to size_t like jsmith said, they need to include whatever has it)
2.) Idr, it's probably an unsigned int or an unsigned long int.
3.) AFAIK, all of the STL containers/stuff that would have a size() member function.
4.) Yes, most of the time.
4.) The warning it because size_t/size_type are unsigned types. They are used when you call the size() member function (and possibly other functions that return size-like variables)
according to here:
http://www.cppreference.com/wiki/string/size

the size member function returns a size_type but when I try to delcare a size_type variable as in size_type a; in my program after including the string library I get this error:
error: ‘size_type’ was not declared in this scope
why is this happening and how do I resolve this problem?

btw, where can I find all the typedefs and function prototypes of each library? I'm using linux...
Last edited on
size_type is always a typedef inside the class.

For example,

std::vector<int> v;
std::vector<int>::size_type sz = v.size();

std::string s;
std::string::size_type sz = s.size();

(I'm going to guess that size_t is unsigned int because I know on my 32-bit platform
it is 32 bits. Meaning on a 64-bit platform it is likely 64 bits.)

so to play safe, I should use size_type whenever size_type is specified as a parameter/return value as on the cpp reference and size_t when specified as such right? so that whenever the implementation changes I wouldn't have to change my program unlike when I used int in the past in place of size_t and size_type
Last edited on
Yes, if you want to be 100% sure it will always work, then use size_type when you can.
Yes, if you want to be 100% sure it will always work, then use size_type when you can.

what about size_t? should I use size_type in place of it too? I guess not right?

and thanks to everyone who has helped
Last edited on
use size_t to hold size_ts and size_type to hold size_types.
btw, where can I find all the typedefs and function prototypes of each library? I'm using linux...

You can usually find them in the headers in /usr/include/c++/<version>/

It can be a little overwhelming at first, but I think poking through the headers helps you to learn. Remember that every compiler doesn't have to do it the same way though.
Topic archived. No new replies allowed.