问题
This question got me thinking about the max_size
method in vector
class. It is quite apparent that practically the number of elements contained in the vector will be much lesser than what max_size
returns. So I was wondering where this will be useful? Any clues?
回答1:
It really isn't very useful.
The only theoretical usage would be to check that if you need a container larger than max_size()
, you are in trouble. But you would probably realize that already when considering a port of your database server to a microwave oven.
The committee once considered improving the function, but didn't find it useful enough to be worth a change:
max_size()
isn't useful for very many things, and the existing wording is sufficiently clear for the few cases that max_size() can be used for. None of the attempts to change the existing wording were an improvement.
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#197
回答2:
I think the cases where it is useful would be rare, but you might have a case where you need a ridiculously large index. For example, maybe you have an array of indices, and you want to initialize it with index values that couldn't possibly be used, indicating those indices haven't been determined yet.
回答3:
It's just to tell you how many elements you can possibly store in a vector in your environment. It may be of importance in embedded environments.
来源:https://stackoverflow.com/questions/9867206/practical-use-of-vectormax-size