What is the difference between size_t and size_type? size_type seems to be related closely with the C++ Standard String Class. Couldn't they have just used size_t instead? I don't understand the difference. Could anyone elaborate please?

6 Years
Discussion Span
Last Post by Frederick2

size_type is just a typedef of size_t in STL containers. For a %100 compatibility, you may want to use size_type, but for the most part it's interchangeable, unless you're following the standard.


Couldn't they have just used size_t instead?

Sure, but there were two reasons to define size_type:

  1. Support custom allocators and different memory management strategies without forcing code changes.
  2. Historically, size_type was added to the STL implementation to deal with a problem on the Intel architecture (no, I don't know what the problem was).

Though if you're using the standard allocator, size_type and size_t are synonymous.


Thanks MosaicFuneral and Narue.

Say, MolsaicFuneral! I've got it about half working modifying my String Class to save the string length in a member variable, instead of constantly calling strlen(), and it looks like something like a 90 to 95% improvement in speed! Can't believe I didn't think of that myhself, its so basic. I've more work to do with it yet though.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.