Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ IntSetLIst的实现_C++ - Fatal编程技术网

C++ IntSetLIst的实现

C++ IntSetLIst的实现,c++,C++,下面是用c实现的IntSetList++ #include <iostream> using namespace std; class IntSetList{ private: int n; struct node{ int val; node *next; node(int v,node *p){val=v;next=p;} }; node *head,*sentinel; node *r

下面是用c实现的IntSetList++

#include <iostream>
using namespace std;

class IntSetList{

private:
    int n;
    struct node{

        int val;
        node *next;
        node(int v,node *p){val=v;next=p;}
    };
    node *head,*sentinel;
    node *rinsert(node *p,int t){

        if (p->val<t){
            p->next=rinsert(p->next,t);


    }
        else   if (p->val>t){
            p=new node(t,p);
            n++;
        }
         return p;
            }



public:
    IntSetList(int maxelens,int maxval){
        sentinel=head=new node(maxval,0);
        n=0;
            }



    int size() { return n;}

    void insert(int t){  head=rinsert(head,t);}
    void report(int *v){

         int j=0;
         for (node *p=head;p!=sentinel;p=p->next)
             v[j++]=p->val;



    }




    void display (int *v){
         for (int i=0;i<sizeof(v)/sizeof(v[0]);i++){
             cout<<v[i];
         }





    }

};

int main(){



    IntSetList s(10,15);
    int v[10];
    s.insert(7);
    s.insert(2);
    s.insert(1);
    s.insert(11);
    s.insert(13);
    s.insert(14);
    s.insert(5);
    s.insert(6);
    s.insert(12);
    s.insert(9);
    s.report(v);

    s.display(v);






     return 0;
}
#包括
使用名称空间std;
类IntSetList{
私人:
int n;
结构节点{
int-val;
节点*下一步;
node(intv,node*p){val=v;next=p;}
};
节点*头,*哨兵;
节点*rinsert(节点*p,int t){
如果(p->valnext=rinsert(p->next,t);
}
否则如果(p->val>t){
p=新节点(t,p);
n++;
}
返回p;
}
公众:
IntSetList(int maxelens,int maxval){
sentinel=头部=新节点(maxval,0);
n=0;
}
int size(){return n;}
void insert(int t){head=rinsert(head,t);}
无效报告(int*v){
int j=0;
用于(节点*p=头部;p!=哨兵;p=p->next)
v[j++]=p->val;
}
无效显示(int*v){

对于(int i=0;i根本没有输出?我怀疑它至少输出了一个数字,因为
sizeof(v)
至少与
sizeof(v[0])
一样大,但可能只是一样大,因为在大多数32位计算机上指针的大小与int相同

sizeof(v)/sizeof(v[0])
技巧仅适用于数组,而不适用于指针。解决此问题的常见技巧是将函数声明为模板,因此:

template <int N>
void display (int (&v)[N])
{
     for (int i = 0; i < N; ++i)
     {
         cout << v[i];
     }
}

您可能想告诉我们您希望/期望它做什么。现在,它看起来非常像一个较慢版本的
std::set
(或者可能是
std::multiset
)。
void display (int *v, int n)
{
     for (int i = 0; i < n; ++i)
     {
         cout << v[i];
     }
}
std::copy(v, v + sizeof(v)/sizeof(v[0]), std::ostream_iterator<int>(std::cout));