Assigning length of a string to an integer: C++

  c++, size, stdstring, unsigned-long-long-int

Using C++, I want to create a for loop as follows:

 for(int i = text.size(); i >= 0; i--) {
 {

‘text’ is a std::string.

Please could somebody help me understand why my complier (xCode) requires me to initialise ‘i’ as an unsigned long, and not as integer?

I assume the following, but I don’t know, and I’d like to develop a better understanding: I believe it is to do with an integer having a maximum value of 2147483647, whereas an unsigned long has a maximum value of 18,446,744,073,709,551,615. Since a string can hold up to 4294967295 characters, the use of an integer is inappropriate since its maximum value couldn’t represent the maximum length of a string?

Finally, is the unsigned long data type appropriate, or should I use a long int?

My amended for loop is as follows:

 for(unsigned long i = text.size(); i >= 0; i--) {
    }

or

for(long int i = text.size(); i >= 0; i--) {
    }

Thank you in advance for for any advice offered,

Ian

Source: Windows Questions C++

LEAVE A COMMENT