I'm not really understanding this completely... I kinda understand how it is working but there are a few things that are confusing me..Such as string::size_type. The book said that it returns the size of something (derp) but i tried outputting the result and it was like a bunch of random number. I didn't count but it was probably around 8 random numbers, for example: 35694625. I don't understand how that gets returned if it says it gets the size of something? o.o

Well the code is taken directly from the book I am reading on C++ and it compiles fine? The book was pretty highly recommended by people on lots of different forums so i'm sure the code is fine its just that I don't really understand what ::size_type is/does. :/

As Endurion said, it is just a typedef. It is the type used by the std::string class to represent a size. For example, the size() member function returns the size of the string, and the type of the value returned is std::string::size_type.

Exactly what it says on the tin: it is the type used to store the size of the string.

In general, std::string::size_type will be a typedef (alias) to size_t, and size_t will be a typedef to an unsigned int.

- What does that mean? It means that you can use string::size_type exactly as you would use an unsigned int.

- If size_type is just an unsigned int, why do we need it? That's probably too technical for this forum. But in brief, there are cases where size_type might not be an unsigned int (it could be an unsigned long instead, on a 64-bit machine). Using size_type lets our code work seamlessly on such a platform.

As you see the SIZE_T is a typdef that stores the integer of length. Where as sizeof function returns the length of the string Greeting.

I'm gonna nitpick this 'cause otherwise it might confuse some beginners. First, there is no SIZE_T. Perhaps were you referring to std::size_t/::size_t (remember C++ is case sensitive)? Second, even if SIZE_T existed, it clearly doesn't "return" anything. It's just the name for a datatype.

std::size_t (and ::size_t) is the type of the result of the sizeof operator. It doesn't have a specific size required by the standard, but it is guaranteed to be able to store and represent the size of any data structure that fits in the machine (which is why on a 32-bit machine, it's usually a 32-bit unsigned integer, and on a 64-bit machine, a 64-bit unsigned integer), which is why it's commonly used as the type of an index into an array/data structure.

std::string::size_type is what std::string uses to represent sizes. As swiftcoder says, it's usually just the same thing as std::size_t. You'll see a lot of containers in the standard library have a size_type defined. One reason this is done is so that if you're writing a templated function that works on some container, you can just do T::size_type (where T is the template parameter) and be sure you're using the right data type. After all, the C++ standard doesn't strictly require them all to be exactly the same type, so this allows you to be really careful.

It is not an unsigned int. But sometimes it might be.It is a type that is defined to be the resulting type of the sizeof() and offsetof() operators, which means compiling for an x86 or other 32-bit system means it will have a range from 0 to 4,294,967,295, whereas on x64 and other 64-bit systems it will have a range from 0 to 18,446,744,073,709,551,615.

But more simply, 32-bit machines and 64-bit machines have different ranges of addresses, and size_t is a type that is guaranteed to cover that whole range, whatever it may be.

Why does string use that type to return the length of the string?Because the length of the string could be longer on a 64-bit machine that is possible on a 32-bit machine. If you used an “unsigned int” in Microsoft® Visual Studio® your string will be limited to 4,294,967,295 characters even if you are building for a 64-bit machine.

To use an integer instead of size_t is always a flaw.If any function returns a size_t, you should always store the result in a size_t unless you have specific constraints (such as additional checks to ensure the string is not longer than 4,294,967,295 characters in length) that allow you to do so.

I'm gonna nitpick this 'cause otherwise it might confuse some beginners. First, there is no SIZE_T.

As you see the SIZE_T is a typdef that stores the integer of length. Where as sizeof function returns the length of the string Greeting.

std::string varString = "Hello World";unsigned int size = sizeof(varString);sizeof DOES NOT RETURN THE SIZE OF THE STRING INSIDE THE STD::STRING

It returns the size of the string class. The string class uses dynamic memory to store cstrings. So only the pointer is counted as well as any other members. If you want the size of an std::string you use .size();

If this post or signature was helpful and/or constructive please give rep.

As you see the SIZE_T is a typdef that stores the integer of length. Where as sizeof function returns the length of the string Greeting.

std::string varString = "Hello World";unsigned int size = sizeof(varString);sizeof DOES NOT RETURN THE SIZE OF THE STRING INSIDE THE STD::STRING

It returns the size of the string class. The string class uses dynamic memory to store cstrings. So only the pointer is counted as well as any other members. If you want the size of an std::string you use .size();

sizeof returns the length in bytes of a char. - whether it be a array or what not. Whatever - I was trying to help out; screw that idea.

sizeof returns the length in bytes of a char. - whether it be a array or what not. Whatever - I was trying to help out; screw that idea.

You are, unfortunately, blatantly wrong on this count.

sizeof() returns the correct size for the given type, or the correct length of a statically allocated array. std::string uses dynamically allocated memory to store the string contents, so sizeof() will not see that memory.

sizeof returns the length in bytes of a char. - whether it be a array or what not. Whatever - I was trying to help out; screw that idea.

You are, unfortunately, blatantly wrong on this count.

sizeof() returns the correct size for the given type, or the correct length of a statically allocated array. std::string uses dynamically allocated memory to store the string contents, so sizeof() will not see that memory.

People don’t lose respect for you for being wrong. They lose respect for you for handling it poorly.

L. Spiro

True. I probably woke up on the wrong side of the bed today. You're right bro, if a game critic gave me poor game review - probably would aggravate me. So, yeah - I agree I have to react more proactive and professional about. I also have to realize critizism comes in a helping manner and less of a attack. I was being defensive and that's not good demonstration for others to see on this forum. Professionalism is the way to lead people in the right direction. Which, again I have to work upon if I am ever going to be taken seriously. So, yeah I agree with the quote above.