Tuesday, December 19, 2017

C++98: std::max_size()

You can query what the maxmum size of container is by using the template function std::max_size(). Here is an example:

#include <iostream>
#include <vector>

int main()
{
  std::vector<char     > Chars ;
  std::vector<short    > Shorts;
  std::vector<int      > Ints  ;
  std::vector<long long> Longs ;

  std::cout << Chars .max_size() << " ";
  std::cout << Shorts.max_size() << " ";
  std::cout << Ints  .max_size() << " ";
  std::cout << Longs .max_size() << " ";

  std::cout << std::endl;
  return 0;
}
// Output: 2147483647 2147483647 1073741823 536870911
// Notes:
//   2147483647 (= 2.0 G - 1 = 31 bits)
//   1073741823 (= 1.0 G - 1 = 30 bits)
//   536870911  (= 0.5 G - 1 = 29 bits)
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, p. 145.

No comments:

Post a Comment