Difference.

This is a discussion on Difference. within the C++ Programming forums, part of the General Programming Boards category; What is the exact difference between these two? Code: const int myNumber = 9; Code: const string::size_type myNumber = 9;...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    62

    Difference.

    What is the exact difference between these two?
    Code:
    const int myNumber = 9;
    Code:
    const string::size_type myNumber = 9;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Obviously the type myNumber differs

    (Especially the fact that int is signed whereas std::string::size_type is an unsigned integer type.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by laserlight View Post
    Obviously the type myNumber differs

    (Especially the fact that int is signed whereas std::string::size_type is an unsigned integer type.)
    So it would be the same as writing the following?
    Code:
    const unsigned int myNumber = 9;
    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.
    Last edited by Kitt3n; 07-19-2010 at 05:28 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Quote Originally Posted by Kitt3n
    So it would be the same as writing the following?
    Maybe, maybe not. For example, std::string::size_type might ultimately be traced to be indirectly a typedef for unsigned long instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Kitt3n View Post
    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.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by EVOEx View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    62
    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?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DIfference between , and ;
    By rocketman03 in forum C++ Programming
    Replies: 6
    Last Post: 10-21-2009, 09:46 AM
  2. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 05:36 PM
  3. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  4. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  5. Replies: 10
    Last Post: 11-06-2005, 08:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21