首页
社区
课程
招聘
[求助]cpp队列模板类有一个地方看不懂啦
发表于: 2013-12-13 19:40 9909

[求助]cpp队列模板类有一个地方看不懂啦

2013-12-13 19:40
9909
/*
   
*/

#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <bitset>
#include <cstring>
#include <iomanip> //cout<<setprecision(3);
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <numeric>
#include <iterator>
#include <memory>

using namespace std;
// function template declaration must precede friend declaration in Queue
template <class T> class Queue;
template <class T>
std::ostream& operator<<(std::ostream&, const Queue<T>&);

//begin{basic-Queue}
template <class Type> class Queue {
    //end{basic-Queue}
    friend std::ostream&
        operator<< <Type> (std::ostream&, const Queue<Type>&);
    public:
    Queue(): head(0), tail(0) { }

    // construct a Queue from a pair of iterators on some sequence
    template <class It> Queue(It, It): head(0), tail(0)
    { copy_elems(beg, end); }

    // copy control to manage pointers to QueueItems in the Queue
    Queue(const Queue &Q): head(0), tail(0) {copy_elems(Q);}
    Queue& operator=(const Queue&);
    ~Queue() { destroy(); }
    // replace current Queue by contents delimited by a pair of iterators
    // MS: requires member templates to be defined inside the class body
    template <class Iter> void assign(Iter beg, Iter end)
    { destroy(); copy_elems(beg, end); }

    // return element from head of Queue
    // unchecked operation: front on an empty Queue is undefined
    Type& front()            { return head->item; }
    const Type front() const { return head->item; }

    void push(const Type &);// add element to back of Queue
    void pop();             // remove element from head of Queue

    bool empty() const {    // true if no elements in the Queue
        return head == 0;
    }
    //begin{basic-Queue}
    // interface functions to Queue are unchanged
    private:
    // public members are ok: QueueItem is a private member of Queue
    // only Queue and its friends may access the members of QueueItem
    struct QueueItem {
        // workaround: MS Compiler requires nested class members
        // to be defined inside the class
        QueueItem(const Type &t): item(t), next(0) { }
        Type item;           // value stored in this element
        QueueItem *next;     // pointer to next element in the Queue
    };

    QueueItem *head;   // pointer to first element in Queue
    QueueItem *tail;   // pointer to last element in Queue
    //end{basic-Queue}

    // utility functions used by copy constructor, assignment and destructor
    void destroy();          // delete all the elements
    void copy_elems(const Queue&); // copy all elements from parameter
    private:
    // copy all elements from input range denoted by iterator pair
    // MS: requires member templates to be defined inside the class body
    template <class Iter> void copy_elems(Iter beg, Iter end)
    {
        while (beg != end) {
            push(*beg);
            ++beg;
        }
    }
    //begin{basic-Queue}
};
//end{basic-Queue}

//web-comment-begin

template <class Type>
    void
Queue<Type>::copy_elems(const Queue &orig)
{
    // copy elements from orig into this Queue
    QueueItem *pt = orig.head;
    while (pt) {     // last element has next pointer == 0
        push(pt->item);  // copy the element
        pt = pt->next;   // get the next one from orig
    }
}

    template <class Type>
Queue<Type>& Queue<Type>::operator=(const Queue &rhs)
{
    if (this != &rhs) {
        // destroy elements on this Queue
        destroy();
        copy_elems(rhs);    // copy from rhs into left-hand Queue
    }
    return *this;
}

template <class Type>
    void
Queue<Type>::destroy()
{
    while (!empty())
        pop();
}

    template <class Type>
void Queue<Type>::push(const Type &val)
{
    // allocate a new QueueItem object
    QueueItem *pt = new QueueItem(val);

    // put item onto existing queue
    if (empty())
        head = tail = pt;   // queue has only one element
    else
    {
        tail->next = pt;     // add new element to end of queue
        tail = pt;
    }
}

//begin{pop-defn}
    template <class Type>
void Queue<Type>::pop()
{
    // pop is unchecked: popping off an empty Queue is undefined
    QueueItem* p = head;     // keep pointer to head so can delete it
    head = head->next;       // head now points to next element
    delete p;                // delete old head element
}
//end{pop-defn}

    template <class Type>
ostream& operator<<(ostream &os, const Queue<Type> &q)
{
    os << "< ";
    typename Queue<Type>::QueueItem *p;
    for (p = q.head; p; p = p->next)
        os << p->item << " ";
    os <<">";
    return os;
}

#define MAIN 1

#if 1==MAIN

Queue<int> queue1;
int main(int argc, const char *argv[])
{
    for (int ix = 0; ix != 1024; ++ix)
        queue1.push(ix);
    for (int ix = 0; ix != 1024; ++ix) {
        int i = queue1.front();  // check next item
        if (i != ix)
            cout << "Something's wrong! i = " << i
                << " ix = " << ix << endl;
        queue1.pop();   // and remove it
    }
    if (!queue1.empty())
        cout << "Queue is not empty but should be!" << endl;
    else
        cout << "OK, queue empty again" << endl;
    for (int ix = 0; ix != 1024; ++ix)
        queue1.push(ix);
    Queue<int> queue2(queue1);  // use copy constructor
    for (int ix = 0; ix != 1024; ++ix) {
        int i = queue2.front();   // check next item
        if (i != ix)
            cout << "Something's wrong! i = " << i
                << " ix = " << ix << endl;
        queue2.pop();  // and remove it
    }
    if (!queue2.empty())
        cout << "queue2 is not empty but should be!" << endl;
    else
        cout << "OK, queue2 empty again" << endl;
    queue2 = queue1;  // use asssignment operator
    for (int ix = 0; ix != 1024; ++ix) {
        int i = queue2.front();
        if (i != ix)
            cout << "Something's wrong! i = " << i
                << " ix = " << ix << endl;
        queue2.pop();
    }
    if (!queue2.empty())
        cout << "queue2 is not empty but should be!" << endl;
    else
        cout << "OK, queue2 empty again" << endl;
    system("pause");
    return 0;
}

#elif 2==MAIN

int main(int argc, const char *argv[])
{
    // the class Queue<int> is instantiated
    // new expression requires that Queue<int> be defined
    Queue<int> *p_qi = new Queue<int>;
    int ival;
    for (ival = 0; ival != 10; ++ival)
        // the member function push() is instantiated
        p_qi->push(ival);
    int err_cnt = 0;
    for (ival = 0; ival != 10; ++ival) {
        // the member function front is instantiated
        int qval = p_qi->front();
        if (ival != qval) err_cnt++;
        p_qi->pop();                // instantiates pop
    }
    if (!err_cnt)
        cout << "!! queue executed ok" << endl;
    else
        cout << "?? " << err_cnt << " Queue errors" << endl;
    delete p_qi;   // deletes elements in the queue
    system("pause");
    return 0;
}

#elif 3==MAIN

int main(int argc, const char *argv[])
{
    Queue<int> qi;
    // instantiates ostream& operator<<(ostream&, const Queue<int>&)
    cout << qi << endl;

    int ival;
    for (ival = 0; ival != 10; ++ival)
        qi.push(ival);   // instantiates Queue<int>::push(int)
    cout << qi << endl;

    int err_cnt = 0;
    for (ival = 0; ival != 10; ++ival) {
        int qval = qi.front();   // instantiates Queue<int>::front()
        if (ival != qval) err_cnt++;
        qi.pop();                // advance to next element
    }

    cout << qi << endl;
    if (!err_cnt)
        cout << "!! queue executed ok" << endl;
    else
        cout << "?? " << err_cnt << " Queue errors" << endl;
   
    system("pause");
    return 0;
}
#endif

/*
1==main

OK, queue empty again
OK, queue2 empty again
OK, queue2 empty again
请按任意键继续. . .

*/


这里能看懂,因为beg和end在参数里

这里我看不懂了,beg和end没在函数参数里,又不是类成员,怎么编译还能通过并运行正常呢
Queue.7z

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

上传的附件:
收藏
免费 0
支持
分享
最新回复 (5)
雪    币: 101
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
牛奔的大神在哪里
2013-12-13 19:56
0
雪    币: 101
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
解决了
#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <bitset>
#include <cstring>
#include <iomanip> //cout<<setprecision(3);
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <numeric>
#include <iterator>
#include <memory>

template<class T> class Queue
{
    public:
    Queue() {};
    ~Queue(){};
    template <class it> Queue(it,it)
    {
        copy_elems(beg,end);
    }
    template<class Iter> void method(Iter b,Iter e)
    {
        copy_elems(b,e);   
    }
   

    template <class Iter> void copy_elems(Iter beg,Iter end)
    {
        for(;beg!=end;++beg)
        {
            cout<<*beg<<" ";
        }       
        cout<<endl;
    }
};

int main(int argc, const char *argv[])
{
    Queue<int> qe;
    cout<<"int array: "<<endl;
    int array[]={1,2,3,4,5,6};
    cout<<"using copy_elems()"<<endl;
    qe.copy_elems(array,array+6);   
    cout<<"using method()"<<endl;
    qe.method(array,array+6);   
   
    cout<<"string array: "<<endl;
    string array_string[]={"1","2","3","4","5","6"};
    cout<<"using copy_elems()"<<endl;
    qe.copy_elems(array_string,array_string+6);
    cout<<"using method()"<<endl;
    qe.method(array_string,array_string+6);
    system("pause");
    return 0;
}

/*

int array:
using copy_elems()
1 2 3 4 5 6
using method()
1 2 3 4 5 6
string array:
using copy_elems()
1 2 3 4 5 6
using method()
1 2 3 4 5 6
请按任意键继续. . .

*/
2013-12-13 20:22
0
雪    币: 101
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
我又彪啦,没仔细想想。
没人理的感觉真不好
2013-12-13 20:25
0
雪    币: 3554
活跃值: (1877)
能力值: ( LV6,RANK:93 )
在线值:
发帖
回帖
粉丝
5
来错地方了,c++问题去csdn之类的比较好。
2013-12-14 00:47
0
雪    币: 4031
活跃值: (47)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
你没看懂的地方是类模板的构造函数,一样在类中的
2013-12-18 14:16
0
游客
登录 | 注册 方可回帖
返回
//