One more question
If I have the following code:
template<class I, class T>
I find (I first, I last, const T& value)
{
while (first != last && *first != value)
++first;
return first;
}
struct int_node {
int val;
int_node *next;
};
// Wrapper
template <class Node>
struct node_wrap {
Node *ptr;
node_wrap(Node* p = 0) : ptr(p) { }
Node& operator *() const { return *ptr }
<<<<< what is this? and more coming up
Node* operator->() const { return ptr }
<<<<< what does cont mean here?
// pre-increment operator
node_wrap& operator++ () { ptr = ptr->next; return
*this; }
// post-increment operator
node_wrap operator++(int) { node_wrap tmp = *this; ++*this;
return tmp; }
bool operator == (const node_wrap& i) const { return ptr ==
i.ptr; }
bool operator != (const node_wrap& i) const { return ptr !=
i.ptr; }
};
bool operator == (const int_node& node, int n) { return node.val == n; }
<<< what are these extra lines for?
bool operator != (const int_node& node, int n) { return node.val != n; }
void main()
{
int_node *list_head, *list_tail;
int_node *in = new(int_node);
in->val = 100;
in->next = 0;
list_head = list_tail = in;
for (int i = 0; i < 10; i++)
{
int_node* in = new(int_node);
in->val = i;
in->next = 0;
list_tail->next = in;
list_tail = in;
}
// 100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
node_wrap<int_node> r;
r = find(node_wrap<int_node>(list_head), node_wrap<int_node>(), 10);
<<<<< don't understand this as well
if ( r != node_wrap<int_node>())
std::cout << (*r).val << std::endl;
r = find(node_wrap<int_node>(list_head), node_wrap<int_node>(), 3);
if (r != node_wrap<int_node>())
std::cout << (*r).val << std::endl;
}
it says that find(list_head, null, 5); won't work, why?
Sorry this is not an English reference, could you please explain the above
code?