first element: 0 or 1?

This is a discussion on first element: 0 or 1? within the C++ Programming forums, part of the General Programming Boards category; As programmers, we're all used to starting off things at 0, not 1. But if I'm designing my template List, ...

  1. #1
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127

    first element: 0 or 1?

    As programmers, we're all used to starting off things at 0, not 1.

    But if I'm designing my template List, and I want the USER to be able to refer to a particular element like so:
    Code:
    list.at(n)
    Should I design so that the FIRST element is list.at(0) or list.at(1)?

    Which do YOU think is more intuitive?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    The user of the class template will be a programmer, right? Starting from 0 would then be more intuitive.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    All the STL implementations, such as vector, starts at element 0. Doing it otherwise will just confuse people.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by matsp View Post
    All the STL implementations, such as vector, starts at element 0. Doing it otherwise will just confuse people.
    To even further confuse people, allow them to set their index base. Personally, I prefer the first element of my arrays to be at index -188573.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    To even further confuse people, allow them to set their index base. Personally, I prefer the first element of my arrays to be at index -188573.
    That will be even more confusing if the default value is ((rand() % 119232712 * (rand() % 3 - 1)).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    It's obvious counting starts at 0 because starting at 1 wastes a perfectly good number, and in a world where integers have finite ranges, squeezing all the usefulness you can out of something so small is important.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by citizen View Post
    It's obvious counting starts at 0 because starting at 1 wastes a perfectly good number, and in a world where integers have finite ranges, squeezing all the usefulness you can out of something so small is important.
    By that account, shouldn't we start with the most negative number?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Quote Originally Posted by matsp View Post
    By that account, shouldn't we start with the most negative number?

    --
    Mats
    Nah, it will loop around to the negative ones once it reaches INT_MAX right?

  9. #9
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    0 it is!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 09:58 AM
  3. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 02:54 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. sorting
    By penny_731729 in forum C Programming
    Replies: 3
    Last Post: 04-28-2003, 11:56 AM

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