堆栈和队列是否像C++中的数组一样传递?



我想问这个问题,因为我们可以在不传递引用的情况下更改传递给函数的数组的内容,但它与堆栈和队列不同。你能说出这两个是如何传递给 然后。

您需要了解 Array 基本上是一系列可通过指针直接访问的内存位置。 与此相反,stacksqueues是容器,由你实例化。

<小时 />
void foo(std::stack<int> st) { //pass by value
//whatever
}
void foo(std::stack<int>& st) { //pass by reference
//whatever
}
void foo(std::stack<int>* st) { //pass by pointer
//whatever
}
std::stack<int> stack; //instantiation
foo(stack); //calling the function
foo(&stack); //when you need to pass by pointer (never used)

最新更新