how to Validate if iterator is not empty???

This is a discussion on how to Validate if iterator is not empty??? within the C++ Programming forums, part of the General Programming Boards category; i have this iterator: Code: std::list<entanim_s>::iterator iterAnim and i want to validate with this iterator is not empty i have ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Question how to Validate if iterator is not empty???

    i have this iterator:
    Code:
     std::list<entanim_s>::iterator iterAnim
    and i want to validate with this iterator is not empty

    i have this code but vs dont compile.
    Code:
    if(iterAnim != NULL)
    {	
        iterAnim->ePriority = priority;
    }
    why? what the reason for this???
    what is the best way to do this???

  2. #2
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    There is no way to test if an iterator is uninitialized, but you can initialize it to the end() of your container as a default invalid value:
    Code:
    std::list<entanim_s>::iterator iterAnim = my_list.end();
    Code:
    if (iterAmin != my_list.end())
    {
        iterAnim->ePriority = priority;
    }

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    An iterator cannot be empty. The list could be empty, or iterAnim could be invalid.

    The official position seems to be that it's your job to make sure iterAnim is always valid. Always initialize your iterators, and if you do something destructive that might invalidate your iterator, make sure you set it to something known good.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    3
    so this code

    Code:
    if(iterAnim != NULL)
    {	
        iterAnim->ePriority = priority;
    }
    is impossible to be valid??

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That can't really work, correct. iterAnim is not really a pointer and can't be NULL.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    3
    thanks for the fast aswers....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to validate an entry, New to C
    By jm0825 in forum C Programming
    Replies: 7
    Last Post: 05-16-2010, 01:04 AM
  2. Is there a way to validate floating value?
    By kekewong in forum C Programming
    Replies: 5
    Last Post: 10-10-2009, 03:21 PM
  3. iterator for empty list
    By KIBO in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2009, 02:24 AM
  4. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  5. validate userinput
    By justdoit22 in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 06:46 PM

Tags for this Thread


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