Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string::size_type instead of int

const std::string::size_type cols = greeting.size() + pad * 2 + 2; 

Why string::size_type? int is supposed to work! it holds numbers!!!

like image 871
Delirium tremens Avatar asked Jul 25 '09 03:07

Delirium tremens


People also ask

What is string :: size_type?

The std::string type defines size_type to be the name of the appropriate type for holding the number of characters in a string. Whenever we need a local variable to contain the size of a string, we should use std::string::size_type as the type of that variable.

What is size_type C++?

size_type is a (static) member type of the type vector<int> . Usually, it is a typedef for std::size_t , which itself is usually a typedef for unsigned int or unsigned long long .

What does std::string () do?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

Is size_type same as Size_t?

size_t is defined as the type used for the size of an object and is platform dependent. container::size_type is the type that is used for the number of elements in the container and is container dependent.


2 Answers

A short holds numbers too. As does a signed char.

But none of those types are guaranteed to be large enough to represent the sizes of any strings.

string::size_type guarantees just that. It is a type that is big enough to represent the size of a string, no matter how big that string is.

For a simple example of why this is necessary, consider 64-bit platforms. An int is typically still 32 bit on those, but you have far more than 2^32 bytes of memory.

So if a (signed) int was used, you'd be unable to create strings larger than 2^31 characters. size_type will be a 64-bit value on those platforms however, so it can represent larger strings without a problem.

like image 59
jalf Avatar answered Sep 19 '22 03:09

jalf


The example that you've given,

const std::string::size_type cols = greeting.size() + pad * 2 + 2; 

is from Accelerated C++ by Koenig. He also states the reason for his choice right after this, namely:

The std::string type defines size_type to be the name of the appropriate type for holding the number of characters in a string. Whenever we need a local variable to contain the size of a string, we should use std::string::size_type as the type of that variable.

The reason that we have given cols a type of std::string::size_type is to ensure that cols is capable of containing the number of characters in greeting, no matter how large that number might be. We could simply have said that cols has type int, and indeed, doing so would probably work. However, the value of cols depends on the size of the input to our program, and we have no control over how long that input might be. It is conceivable that someone might give our program a string so long that an int is insufficient to contain its length.

like image 37
mihai Avatar answered Sep 22 '22 03:09

mihai