CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Resolved How to convert from a const iterator to a non-const iterator? [Resolved]

    Ok, here's the below snippet that's causing the problem.
    Code:
    bool graph::find_node(const std::string & node_name, const std::list<node * > & list)
    {
     for(std::list<graph::node * >::iterator iter = list.begin(); iter != list.end(); iter++)
     {
      if(( * ( * iter)).name == node_name)
      {
       return true;
      }
     }
    
     return false;
    }
    Now the list that I'm using is a constant and I would like it to remain like that in order to prevent it from being changed in any way. However, it seems that the iterator that is created from it (in the boldened line) is also a const. How could I create a non-const iterator in this instance? Or convert the const iterator to a non-const one after creation?

    [edit]

    I could change the passed in list to a non-const and it'll work, but I would not like to break good programming practices in the process, which is the reason for the question.
    Last edited by YourSurrogateGod; January 12th, 2006 at 04:48 PM.
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  2. #2
    Join Date
    Dec 2005
    Posts
    642

    Post Re: How to convert from a const iterator to a non-const iterator?

    You don't need a non-const iterator. Just change the emphasized line to
    Code:
    for(std::list<graph::node * >::const_iterator iter = list.begin(); iter != list.end(); iter++)
    Last edited by googler; January 12th, 2006 at 04:54 PM.

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: How to convert from a const iterator to a non-const iterator?

    a const_iterator means that you cannot change the object that iterator points to and this is what you want, since the list is const and hence not changeable. If you were to have a non-const iterator then you would break the contract of passing a const list.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: How to convert from a const iterator to a non-const iterator?

    Why are you writing your own loop? What is wrong with std::find_if ?

    Of course, it requires a functor. You can write one thus:

    Code:
    class node_name_is
    {
       std::string searchName; // can also be const std::string & 
    public:
       node_name_is( const std::string & sn ) : searchName( sn )
       {
       }
      
        bool operator() ( graph::node * pNode )
        {
            return searchName == pNode->name;
        }
    };
    
    
    list<graph::node*> const_iterator it =
       std::find_if( nodelist.begin(), nodelist.end(),
               node_name_is( node_name ) );
    
    // check if it= nodelist.end()
    (I don't suggest you call your list list, although as long as you don't force namespace std in, there is no name-clash).

  5. #5
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: How to convert from a const iterator to a non-const iterator?

    Quote Originally Posted by NMTop40
    Why are you writing your own loop? What is wrong with std::find_if ?

    Of course, it requires a functor. You can write one thus:

    Code:
    class node_name_is
    {
       std::string searchName; // can also be const std::string & 
    public:
       node_name_is( const std::string & sn ) : searchName( sn )
       {
       }
      
        bool operator() ( graph::node * pNode )
        {
            return searchName == pNode->name;
        }
    };
    
    
    list<graph::node*> const_iterator it =
       std::find_if( nodelist.begin(), nodelist.end(),
               node_name_is( node_name ) );
    
    // check if it= nodelist.end()
    Thanks. I couldn't find that method here, so I didn't know that it existed.
    Quote Originally Posted by NMTop40
    (I don't suggest you call your list list, although as long as you don't force namespace std in, there is no name-clash).
    That's not one of my better examples of coding skills .
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  6. #6
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: How to convert from a const iterator to a non-const iterator?

    Quote Originally Posted by NMTop40
    Why are you writing your own loop? What is wrong with std::find_if ?

    Of course, it requires a functor. You can write one thus:

    Code:
    class node_name_is
    {
       std::string searchName; // can also be const std::string & 
    public:
       node_name_is( const std::string & sn ) : searchName( sn )
       {
       }
      
        bool operator() ( graph::node * pNode )
        {
            return searchName == pNode->name;
        }
    };
    
    
    list<graph::node*> const_iterator it =
       std::find_if( nodelist.begin(), nodelist.end(),
               node_name_is( node_name ) );
    
    // check if it= nodelist.end()
    (I don't suggest you call your list list, although as long as you don't force namespace std in, there is no name-clash).
    I have another question. Why did you overload the () operator?
    Last edited by YourSurrogateGod; January 12th, 2006 at 11:04 PM.
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  7. #7
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: How to convert from a const iterator to a non-const iterator?

    You should read about functors.
    here is a link http://www.newty.de/fpt/functor.html
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: How to convert from a const iterator to a non-const iterator?

    Quote Originally Posted by YourSurrogateGod
    I have another question. Why did you overload the () operator?
    ...because that is how a functor or a function object is defined. The name is very intuitive - function object. It's an object that behaves syntactically like a function and to make it look so, we have to overload the "()" operator. They are superior, object oriented alternatives to function pointers.

    std::find_if is not a member of std::list. It is available under the header <algorithm>. Hope this helps. Regards.

  9. #9
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: How to convert from a const iterator to a non-const iterator?

    Quote Originally Posted by exterminator
    ...because that is how a functor or a function object is defined. The name is very intuitive - function object. It's an object that behaves syntactically like a function and to make it look so, we have to overload the "()" operator. They are superior, object oriented alternatives to function pointers.

    std::find_if is not a member of std::list. It is available under the header <algorithm>. Hope this helps. Regards.
    That's cool, but why not the '==' operator? I figured that this would be more useful since the program will use comparison.

    Basically I don't understand what find_if does on the inside with the () operator.
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  10. #10
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: How to convert from a const iterator to a non-const iterator?

    find_if works with what's called a predicate. Basically, a predicate takes two inputs and returns a bool. What comparisons the predicate does on the two inputs is up to it, as long as it returns a deterministic result (i.e. it always returns the same answer for the same input).

    The == operator is a predicate:
    Code:
    bool operator==(lhs, rhs)
    But, then, so is operator>, operator<, operator!= and many others. Or you can write your own (for example to compare two structs by a subset of their members).

    std::find is just std::find_if with operator== as the predicate. Or, to put another way, find_if is an extension of find that allows you to use more complicated rules for determining whether you've found what you want.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured