Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/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++ 我的pop函数应该采用什么参数?_C++_Templates_Stack - Fatal编程技术网

C++ 我的pop函数应该采用什么参数?

C++ 我的pop函数应该采用什么参数?,c++,templates,stack,C++,Templates,Stack,这是我的函数 template <class KeyType > KeyType * Stack<KeyType>::Pop(KeyType& x) { if (IsEmpty()) { //isempty is just a bool function StackEmpty(); //just prints out that stack is empty return 0;

这是我的函数

    template <class KeyType >
    KeyType * Stack<KeyType>::Pop(KeyType& x) {
        if (IsEmpty()) {  //isempty is just a bool function
            StackEmpty(); //just prints out that stack is empty
            return 0;     //bad coding breaking out of the function
        }
        x = stack[top--]; //stack is a pointer to an array, top is the top of the stack
        return &x;
    }
模板
KeyType*Stack::Pop(KeyType&x){
if(IsEmpty()){//IsEmpty只是一个bool函数
StackEmpty();//只需打印出堆栈为空
返回0;//函数出现错误编码
}
x=stack[top-->;//stack是指向数组的指针,top是堆栈的顶部
return&x;
}
我的问题是:
我不确定这在总体上会被称为什么。据我所知,pop函数实际上不应该有从堆栈中弹出什么的选项。后进先出对吗?主要的问题是Keytype&x参数具体是什么,以及在main中如何调用它?(在这种情况下,KeyType被初始化为KeyType*在这个特定程序中堆栈一个int)。

它填充弹出项的值

int main(..)
{

   ...
   int poppedItem;

  stack.pop(poppedItem);
}

如果
KeyType
参数如您所说是
int
,则您的
堆栈可能如下所示:

Stack<int> stack;
这是一个设计非常奇怪的函数

Stack
是一个类模板,由存储在堆栈上的类型参数化(出于某种原因名为
KeyType
)。该函数将类型引用的输出参数
x
用于
KeyType
,如果堆栈不是空的,则将弹出的值分配到
x
。同时,它返回其地址(返回指向
KeyType
)的指针)。如果调用
pop()
时堆栈为空,它将调用
StackEmpty()
,然后返回空指针

用法:

int main() {
  Stack<int> stack;
  //fill stack somehow
  int val;
  stack.pop(val);  //val will be set to the popped item, or unchanged if the stack was empty

  // You can also use the return value to access the popped item:
  std::cout << *stack.pop(val);

  // ... or use it to test whether the pop() succeeeded
  if (stack.pop(val)) {
    //val was popped, use it
  }
}
intmain(){
堆叠;
//以某种方式填充堆栈
int-val;
stack.pop(val);//val将设置为弹出的项,如果堆栈为空,则保持不变
//您还可以使用返回值访问弹出的项目:

std::cout变量x与返回值相同(只是获取从堆栈中排除的顶部元素的另一种方法)

Stack my_Stack;
//废话废话。。。
int tmp;
int*tmp\u pointer=my\u stack.pop(tmp);
一些函数(tmp);
一些其他函数(*tmp\u指针);
//tmp_指针==&tmp;
//您可以使用两种方法之一

据我所知,该函数接受任何键类型的元素并检索引用

这么叫

int value = 0; 
Pop(value);
使用&value调用Pop-实际上是使用int值的地址,因此是通过引用调用的


如果您使用编译器可能会告诉您的任何非数字数据类型调用Pop,返回语句无效,我想知道
返回0
的情况。可能返回NULL会更好。(至少更好阅读)

实际上,堆栈的Pop方法不应该接受任何参数,因为它总是返回堆栈中最顶层的元素,因为它是后进先出的数据结构。我认为您的Pop方法将返回指向堆栈顶层元素以及值本身的指针;第一个作为返回值,后一个通过引用传递。它实际上不是一个堆栈,它是一个使用类的堆栈实现,可能应该放在那里的某个地方,但这是一个教练给我们的头文件,所以我认为它是可行的。我一直在争论是否只删除参数一段时间lol。谢谢你的解释,先生,是的,我不会像这样设置它这只不过是我们的老师给我们的一个基本堆栈模板的一部分,我们可以用来做家庭作业,在这里我们必须做其他的函数,而我不能把我的头绕在它上面。
Stack<int> my_stack;

// blah-blah-blah ...

int tmp;
int* tmp_pointer = my_stack.pop(tmp);
some_func(tmp);
some_other_func(*tmp_pointer);

// tmp_pointer == &tmp;  
// you can use one of two ways
int value = 0; 
Pop(value);