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 03:48 PM.
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.
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.
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.
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
Bookmarks