What is the exact difference between these two?
Code:const int myNumber = 9;Code:const string::size_type myNumber = 9;
What is the exact difference between these two?
Code:const int myNumber = 9;Code:const string::size_type myNumber = 9;
Obviously the type myNumber differs
(Especially the fact that int is signed whereas std::string::size_type is an unsigned integer type.)
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So it would be the same as writing the following?
The book tells me that std::string::size_type can hold the size of any string and that kind of confuses me because they use it to hold an integer in one of the example codes if I am right.Code:const unsigned int myNumber = 9;
Last edited by Kitt3n; 07-19-2010 at 05:28 AM.
Maybe, maybe not. For example, std::string::size_type might ultimately be traced to be indirectly a typedef for unsigned long instead.Originally Posted by Kitt3n
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
What is so confusing? The size, or length, of a string is a number, an integer, yes? Then why is it so confusing to create an integer of size_type that holds a number?
size_type is simply an integral type of sufficiently large size to be able to hold the size of all kinds of strings. For example, it wouldn't be signed because a size is always positive (therefore, it makes no sense to have a negative type).
That's true: std::string::size_type can hold the size of any string. Can "unsigned int"? Maybe, maybe not. Usually, yes. And as stated before, I think they usually are the same, but not always.
In reality, I've never seen a string of 2^32 bytes or more, and as an unsigned int is commonly 32 bits, an unsigned int will very likely be able to hold the size of any string as well.
However, imagine that on an architecture an int is 16 bits. The maximum value of an unsigned int is 65535, then. And it's quite easily possible to have strings of more than 64KiB. In that case, an unsigned int won't be able to hold it, where std::string::size_type is guaranteed to be able to.
That's not quite true.
A string::size_type (as with the size_type for other containers) is only guaranteed to support a range of values consistent with the machine's memory model. A 16-bit system, by definition, only requires a size_type 16 bits in size.
Note also that there is no relationship between the size of the int type and the memory model: although, in practice, the size of an int is often consistent with the memory model (eg a 16 bit memory system may support a 16 bit int type) the standard does not require that.
I think I got it now. I can not find the answers to the questions in the book anywhere either so I will make a topic for that hope someone can look over them.
Btw. what is the best compiler for C++. I am using CodeBlocks at the moment and I am wondering if there are any better out there. And for what reason are they better?
Best is usually subjective. GCC and Visual C++ are two known very good compilers out there, though. Note that Code::Blocks is an IDE, and not a compiler. In fact, the IDE can use both aforementioned compilers.
Visual C++ comes with the Visual Studio IDE. You will have to make up your own mind to which one you like more.
Although GCC is known to be more picky about standard compliant code, especially when it comes to the tricky stuff such as templates. Otherwise there is little difference.