C++ 在C++; 第一个问题:

C++ 在C++; 第一个问题:,c++,arrays,queue,C++,Arrays,Queue,所以,我正在学习堆栈和队列。并尝试在C++中实现自己的队列。 现在我无法对数组进行排队。以下是我使用的普通代码: //g++5.4.0 #include <iostream> using namespace std; #define SIZE 10 template <class Eltype> class Queue { /* * MAXSIZE carries the max no of element a queue can have.

所以,我正在学习堆栈和队列。并尝试在C++中实现自己的队列。 现在我无法对数组进行排队。以下是我使用的普通代码:

//g++5.4.0

#include <iostream>    
using namespace std;
#define SIZE 10
template <class Eltype>
class Queue {
    /*
     *  MAXSIZE carries the max no of element a queue can have.
     *  nItem  carries the no of element queue have.
     *   front carries current front index
     *   rear carries current rear index.
     *   """" CIRCULAR  QUEUE """
     */
    int MAXSIZE, front, rear, nItem;
    Eltype queue[1000];
    public:
        Queue (int size = SIZE) { //constructor:-)
           // queue = &base;
            MAXSIZE = size;
            front = -1;
            rear = -1;
            nItem = 0;
        }
    int push(Eltype n) {
        //pushes element in queue
        if (null()) { 
            //queue is null so front = rear=-1
            front++;
            rear++;
             // adding alement at 0
            queue[rear] = n;
            nItem++;
        } else if (!full()) { 
            // queue is not null as well as it is not full
            if (rear != MAXSIZE - 1) { 
                // checking that rear is not the end
                //if not, adding an element at rear++;
                rear++;
                queue[rear] = n; 
                nItem++;
            } else {
                /* Imagine a situation where first two element is empty but 3 ,4 ,5 is full.
                 * so. without this condition it can give Queue overflow.
                 * but intialising rear by zero can solve this problem
                 */
                // means rear == MAXSIZE but queue is not full so
                rear = 0; 
                // initialisin rear by 0;
                queue[rear] = n;
                nItem++;
            }
        } else {
            cout << "Queue overflow";
        }
        return 1;
    }
    int pop() {
        // /deleting  an element from queue.
        if (null()) { 
            // its already null. No need to be popped;
            cout << "Queue underflow";
        } else { 
            // it is not null
            if (front != MAXSIZE) { 
                // checking that front is not equal to end (not rear). rear and end are two different terminologies.
                // current index element is equal to null
                queue[front]= NULL; 
                front++;
                nItem--;
            } else { 
                // means queue is not null and front = rear ("Circural Queue condition")
                // so , front = 0;
                front = 0; 
                queue[front] = NULL;
                nItem--;
            }

        }
        return 1;
    }
    void printall() { 
        // printing all elements in queue
        int i;
        for (i = 0; i < MAXSIZE; i++) {
            cout << " " << queue[i];
        }
    }
    void debug() { 
        // printting terminologies fro debugging.
        cout << "nItems : " << nItem << "front :" << front << "rear :" << rear;
    }
    private:
        int full() {
            // checking if full
            return ((nItem == MAXSIZE) ? 1 : 0); 
        }
    int null() {
        (nItem <= 0) ? front = rear = -1 :0;
        // checking if null.
        return ((nItem <= 0) ? 1 : 0); 
    }

};
int main() {
    int a[] = {1,2,3};
    Queue<int> q;
    q.push(1);
    q.printall();
    return 0;
}
#包括
使用名称空间std;
#定义尺寸10
样板
类队列{
/*
*MAXSIZE包含队列可以具有的最大元素数。
*nItem携带队列中的元素号。
*前端携带当前前端索引
*后方携带当前后方索引。
*“”循环队列“”
*/
int最大尺寸、前部、后部、nItem;
Eltype队列[1000];
公众:
队列(int size=size){//构造函数:-)
//队列=&base;
最大尺寸=尺寸;
正面=-1;
后部=-1;
nItem=0;
}
int推送(Eltype n){
//在队列中推送元素
如果(null()){
//队列为空,因此前=后=-1
前端++;
后++;
//在0处添加alement
队列[后部]=n;
nItem++;
}如果(!full()){
//队列既不为空也不为满
如果(后!=MAXSIZE-1){
//检查后部是否不是末端
//如果不是,则在后部添加一个元素++;
后++;
队列[后部]=n;
nItem++;
}否则{
/*想象一下,前两个元素是空的,但3、4、5是满的。
*因此,如果没有这个条件,它可能会导致队列溢出。
*但是,将后车初始化为零可以解决这个问题
*/
//表示后部==MAXSIZE,但队列未满,所以
后部=0;
//后面的初始值为0;
队列[后部]=n;
nItem++;
}
}否则{

cout您在内部使用数组来存储元素。数组要求每个元素都一样大。这对于例如整数来说很好。但是对于数组这样的东西,它不起作用,因为数组可以有不同的大小

因此,您不能将队列与
int[]
一起使用。但是,您可以使用
std::vector

内置的
std::queue

C++符号也是如此,区分大小写!在上一段代码中,您忘记了
#include
。@HolyBlackCat在@Khushit Shah上运行时没有它。头文件可能会被其他文件临时包含。这并不意味着您不应该自己包含它们,如果您想编写可移植代码的话。@飞翔,我不能支持你的评论,但你回答了我的问题。谢谢。我甚至试过指针。这是链接[/link]@KhushitShah,你可以在这里看到它是有效的:你的链接断开了,所以我无法检查,但你可能做了其他错误的事情。谢谢,但它仍然不能处理字符串或字符数组:-并且你使用了向量而不是数组。@KhushitShah,是的,就像我在回答中解释的。它不能处理数组。指向数组的指针,或者类似的,已经存在反恐怖主义者将发挥作用。
source_file.cpp: In instantiation of ‘class Queue<int []>’:
source_file.cpp:108:22:   required from here
source_file.cpp:17:26: error: creating array of ‘int []’
         Eltype queue[1000];
source_file.cpp:26:13: note:   initializing argument 1 of ‘int Queue<Eltype>::push(Eltype) [with Eltype = int []]’
         int push(Eltype n) {
source_file.cpp:107:13: warning: unused variable ‘a’ [-Wunused-variable]
         int a[] = {1,2,3};
             ^
source_file.cpp: In instantiation of ‘int Queue<Eltype>::push(Eltype) [with Eltype = int []]’:
source_file.cpp:109:17:   required from here
source_file.cpp:33:22: error: using invalid field ‘Queue<Eltype>::queue’
                 queue[rear] = n;
                      ^
source_file.cpp:41:26: error: using invalid field ‘Queue<Eltype>::queue’
                     queue[rear] = n; 
                          ^
source_file.cpp:51:26: error: using invalid field ‘Queue<Eltype>::queue’
                     queue[rear] = n;
                          ^
source_file.cpp: In instantiation of ‘void Queue<Eltype>::printall() [with Eltype = int []]’:
source_file.cpp:110:20:   required from here
source_file.cpp:87:37: error: using invalid field ‘Queue<Eltype>::queue’
                 cout << " " << queue[i];
//g++  5.4.0

#include <iostream>
using namespace std;

int main()
{
    int a ={1,2,3};
    queue<int[3]> q;
    q.push(a);
    return 0;
}

source_file.cpp: In function ‘int main()’:
source_file.cpp:8:18: error: scalar object ‘a’ requires one element in initializer
     int a ={1,2,3};
                  ^
source_file.cpp:9:5: error: ‘queue’ was not declared in this scope
     queue<int[3]> q;
     ^
source_file.cpp:9:11: error: expected primary-expression before ‘int’
     queue<int[3]> q;
           ^
source_file.cpp:10:5: error: ‘q’ was not declared in this scope
     q.push(a);
     ^