Thread: first element: 0 or 1?

  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
    28,413
    The user of the class template will be a programmer, right? Starting from 0 would then be more intuitive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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, 08:58 AM
  3. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. sorting
    By penny_731729 in forum C Programming
    Replies: 3
    Last Post: 04-28-2003, 10:56 AM