Thread: Question about string::size()

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question about string::size()

    Is string::size() 0-based (i.e. it counts the first character of a string as 0, the second as 1, etc.), or is it 1-based (i.e. it counts the first character of a string as 1, the second as 2, etc.)? I looked in the string reference online, but I wasn't able to find the answer to my question.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    1- based

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Programmer_P View Post
    Is string::size() 0-based (i.e. it counts the first character of a string as 0, the second as 1, etc.), or is it 1-based (i.e. it counts the first character of a string as 1, the second as 2, etc.)? I looked in the string reference online, but I wasn't able to find the answer to my question.
    There's really no ambiguity about the concept of size. If something has N things in it, it has N things in it. If you ever actually encounter a piece of code where a single thing is counted as 0, find the author and hit him with a book.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Three lines of code would answer that but the number of characters is the number of characters, regardless of where you start counting from..

    BTW I was just watching Cloudy with a Chance of Meatballs with the wife and noticed the hero of the story was using (more like abusing) C++ to program his food generations. I mean the code itself didn't make sense ( cin >> hamburger; wtf?) and some of it was correct but could never execute (// N++; again, wtf?) and some was just plain bad (cin >> lettuce; v; bacon

    Still it was better than Jurassic Park, Independence Day and most other computer movies. Matrix 2 was a decent use of nmap (yay!) Wargames in principle was OK (I wrote a wardialer after seeing it and it worked pretty well, found a couple of ATMs).
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by brewbuck View Post
    There's really no ambiguity about the concept of size. If something has N things in it, it has N things in it.
    A very good answer - QFT!

    The real question then is therefore about the behaviour of operator[].
    Anything in C++ concerning arrays is zero-based.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  2. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM