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?

Recommended Answers

All 3 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.