-
January 12th, 2006, 04:46 PM
#1
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.
-
January 12th, 2006, 04:51 PM
#2
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.
-
January 12th, 2006, 05:11 PM
#3
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.
-
January 12th, 2006, 06:36 PM
#4
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).
-
January 12th, 2006, 07:23 PM
#5
Re: How to convert from a const iterator to a non-const iterator?
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.
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 .
-
January 12th, 2006, 10:53 PM
#6
Re: How to convert from a const iterator to a non-const iterator?
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.
-
January 12th, 2006, 11:35 PM
#7
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."
-
January 13th, 2006, 01:45 AM
#8
Re: How to convert from a const iterator to a non-const iterator?
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 13th, 2006, 09:44 AM
#9
Re: How to convert from a const iterator to a non-const iterator?
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.
-
January 13th, 2006, 09:54 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|