size_type

This is a discussion on size_type within the C++ Programming forums, part of the General Programming Boards category; The book I am currently reading (Accelerated C++) talks about using size_type Code: vector<int>::size_type size = vec.size(); instead of an ...

  1. #1
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96

    size_type

    The book I am currently reading (Accelerated C++) talks about using size_type
    Code:
    vector<int>::size_type size = vec.size();
    instead of an integer
    Code:
    int size = vec.size();
    to hold the size of a string or vector. This is the first time I have seen anything on this and have always used just an integer. I was wondering how important it is to use the size_type over just an integer?
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    size_type makes sure that it can hold the largest number that corresponds to the largest data structure. size_type is an integer that can hold a number than an integer may not be able to hold. It says that in the "why size_type" part in bold in that same book not to far from where it first appears.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,704
    I was wondering how important it is to use the size_type over just an integer?
    It's not really.

    The reason that size_t is useful though is because it separates the placement, size, or length of items from regular numbers like ints. Also, the size of anything will never be below 0 so it makes sense to use size_t for that sort of thing.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    In toy programs, it probably makes no real difference - but that doesn't mean you shouldn't get into the habit of using it. Aside from the fact that, as indigo0086 said, there's a possibility that int and size_t are not the same, self-documenting code is a very useful asset. When you begin to write larger projects spanning across several source files, typedefs such as size_t are POD types with added context - using them can make the reader's life just a little bit easier instead of the POD type itself.
    Its not just for yourself either (when you return to code 6 months down the line after a break, you will inevitibly forget what certain parts of the code actually do), but for other people who you may call upon to help if you get stuck at any particular point, its even more important that they can understand what thought processes you were going through when you created your code.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    A size_type is an unsigned integer, as well, which citizen sort of hinted at.

    Using an int may cause problems (warnings at least) when comparing to or assigning from things like vector.size(), which is a vector<whatever>::size_type.
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

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