Thread: simple question - another way

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    simple question - another way

    Hello

    I have:

    Code:
    std::list<std::string> _list;
    std::list<std::string>::iterator it = _list.begin();
    How can I write this in one line:
    Code:
    ++it;
    if (it == list.end()) { it = list.begin(); }
    something like:

    Code:
    it = (it == list.end()) ? list.begin() : ++it;
    will make it crash when it comes to the end of list..

    Thanks for help

  2. #2
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by l2u View Post
    something like:

    Code:
    it = (it == list.end()) ? list.begin() : ++it;
    will make it crash when it comes to the end of list..
    If I'm understanding your problem correctly...

    Code:
    if (++it == list.end()) it = list.begin();
    Incidentally, you shouldn't start identifier names with underscores. Those are generally reserved for the compiler's internal use.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Why try to make it shorter ? Shorter code, in this situation, yields only more errors. The problem occurs because pre-increment and post-increment have a higher operator precedence than the if-else ternary operator ? : . You want to use it += 1. I advise you to use the longer version though. It is much easier to read. Also try indenting your code more (2 braces on a single line.. *sigh*) and try not to use underscores before a variable name, as they are reserved for the implementor, if I remember.

  4. #4
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Not to start a flamewar or anything, but I'd argue that it++ is easier to read, and to any experienced C/C++ programmer, makes it clear that it is an increment, as opposed to a simple multiple of 1 that might be changed to 2 or 3 at another time. As for putting it all in one line, perhaps that is not the best idea.
    Programming Your Mom. http://www.dandongs.com/

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    if (++it == list.end()) it = list.begin();
    Seems readable.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    += 1 doesn't work for std::list iterators anyway.

    What is the exact problem you want to solve? I find none of the snippets posted so far readable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    ++it;
    if (it == list.end()) { it = list.begin(); }
    and
    Code:
    it = (it == list.end()) ? list.begin() : ++it;
    do two different things. One increments before the check against end(), the other increments after.

    I prefer:
    Code:
    if (++it == list.end())
        it = list.begin();
    or
    Code:
    if (it == list.end())
        it = list.begin();
    ++it;
    depending on which you meant to do.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    Is there any way to check if interator has higher operator precedence (if it is bad)?

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    iterators aren't operators, therefore they have no precedence.

    You can look up the iterator precedences in your favourite C++ reference. They cannot be changed, and new iterators cannot be defined.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM