首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

STL 里面的一个有关问题

2012-02-25 
STL 里面的一个问题我刚刚开始看《泛型编程与STL》这本书写了下面的代码可是编译不过去大家帮忙看看应该怎么

STL 里面的一个问题
我刚刚开始看   《泛型编程与STL》   这本书
写了下面的代码     可是编译不过去     大家帮忙看看       应该怎么改
=======================================================
#include   <iostream>
using   namespace   std;

struct   int_node
{
              int   val;
              int_node   *   next;
              //bool   operator==(const   int_node&   node,   int   n){   return   node.val   ==  
n;}
};

template   <typename   Node>
struct   node_wrap
{
              Node   *   ptr;

              node_wrap(Node*   p=0)   :   ptr(p){}
              Node&   operator*   ()   const{   return   *ptr;}
              Node*   operator-> ()   const{   return   ptr;   }

              node_wrap&   operator++()   {ptr   =   ptr-> next;   return   *this;}
              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   i){   return   ptr-> val   ==   i;}
};

int   main()
{
        int_node   t1,t2,t3,t4,t5;
        node_wrap <int_node>   tmp;

        t1.val   =   12;
        t2.val   =   12;
        t3.val   =   12;
        t4.val   =   9;
        t5.val   =   12;

        t1.next   =   &t2;
        t2.next   =   &t3;
        t3.next   =   &t4;
        t4.next   =   &t5;
        t5.next   =   NULL;

        /*tmp   =   */find(node_wrap <int_node> (&t1),   node_wrap <int_node> (),   9);

        //printf( "%d   \n ",   tmp.ptr-> next-> val);
        //cout < <itoa(tmp.next-> val) < <endl;

        system( "pause ");
        return   0;
}



[解决办法]
你还是找些关于运算符重载是选择成员还是友元的资料,与其关注它们在参数形式上的区别吧。
[解决办法]
《泛型编程与STL》我没看过,不过你的代码很有问题。
1.用find算法要 <algorithm> 头文件;
2.int_node 的 operator== 应该为全局;
3.find算法的前两个参数必须是iterator,所以这里要加上


: public std::iterator <std::forward_iterator_tag, Node>

#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;

struct int_node
{
int val;
int_node * next;
};

bool operator==(const int_node& node, int n){ return node.val == n;}

template <typename Node>
struct node_wrap : public std::iterator <std::forward_iterator_tag, Node>
{
Node * ptr;

node_wrap(Node* p=0) : ptr(p){}
Node& operator* () const{ return *ptr;}
Node* operator-> () const{ return ptr; }

node_wrap& operator++() {ptr = ptr-> next; return *this;}
node_wrap operator++(int) {node_wrap tmp = *this; ++*this; return tmp;}

bool operator!=(const node_wrap& i) const {return ptr != i.ptr;}
};

int main()
{
int_node t1,t2,t3,t4,t5;
node_wrap <int_node> tmp;

t1.val = 12;
t2.val = 12;
t3.val = 12;
t4.val = 9;
t5.val = 12;

t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
t5.next = NULL;

tmp = find (node_wrap <int_node> (&t1), node_wrap <int_node> (), 9);

cout < < tmp.ptr-> next-> val < < endl;

return 0;
}

热点排行