C+中数组队列的前端+;将数组推入队列时STL发生更改 我试图在C++ STL中实现数组队列,但是每当我把一个新数组推到队列中并尝试访问队列的前面时,它就会返回最近数组值的值。这是我正在使用的代码 queue<int*> a; int temp[2]; temp[0]=1; temp[1]=1; a.push(temp); cout<<"front "<<a.front()[0]<<"back "<<a.back()[0]<<"\n"; cout<<"size: "<<a.size()<<"\n"; temp[0] = 2; temp[1] = 2; a.push(temp); cout<<"front "<<a.front()[0]<<"back "<<a.back()[0]<<"\n"; cout<<"size: "<<a.size()<<"\n"; temp[0] = 3; temp[1] = 3; a.push(temp); cout<<"front "<<a.front()[0]<<"back "<<a.back()[0]<<"\n"; cout<<"size: "<<a.size()<<"\n";

C+中数组队列的前端+;将数组推入队列时STL发生更改 我试图在C++ STL中实现数组队列,但是每当我把一个新数组推到队列中并尝试访问队列的前面时,它就会返回最近数组值的值。这是我正在使用的代码 queue<int*> a; int temp[2]; temp[0]=1; temp[1]=1; a.push(temp); cout<<"front "<<a.front()[0]<<"back "<<a.back()[0]<<"\n"; cout<<"size: "<<a.size()<<"\n"; temp[0] = 2; temp[1] = 2; a.push(temp); cout<<"front "<<a.front()[0]<<"back "<<a.back()[0]<<"\n"; cout<<"size: "<<a.size()<<"\n"; temp[0] = 3; temp[1] = 3; a.push(temp); cout<<"front "<<a.front()[0]<<"back "<<a.back()[0]<<"\n"; cout<<"size: "<<a.size()<<"\n";,c++,arrays,stl,queue,C++,Arrays,Stl,Queue,有人能解释它为什么会这样吗?你在推指针,而不是数组。指针都是一样的。您从不复制任何数据。使用std::queue一切都会正常。[Pro提示]停止使用指针和原始数组。如果您希望队列包含数组,请使用std::queue或std::queue@MaxLanghof谢谢我收到了:)@NathanOliver谢谢你的提示:) front 1back 1 size 1 front 1back 2 size 2 front 1back 3 size 3

有人能解释它为什么会这样吗?

你在推指针,而不是数组。指针都是一样的。您从不复制任何数据。使用
std::queue
一切都会正常。[Pro提示]停止使用指针和原始数组。如果您希望队列包含数组,请使用
std::queue
std::queue
@MaxLanghof谢谢我收到了:)@NathanOliver谢谢你的提示:)
front 1back 1
size 1
front 1back 2
size 2
front 1back 3
size 3