Возможно, это довольно простой вопрос, но я не могу понять, почему результат такой, какой он есть. Рассмотрим следующий простой код:
#include <iostream>
#include <queue>
void print_queue(std::queue<int> q)
{
while( !q.empty() )
{
std::cout << q.front();
q.pop();
}
std::cout << std::endl;
std::cout << "The size of the queue in the print function: " << q.size() << std::endl;
}
int main() {
std::queue<int> my_queue;
for ( const int &item : {1, 2, 3, 4, 5, 6, 7} )
{
q.push(item);
}
print(my_queue);
std::cout << "The size of the queue in the main loop: " << my_queue.size() << std::endl;
return 0;
}
Ожидаемый результат этого будет:
1 2 3 4 5 6 7
The size of the queue in the print function: 0
The size of the queue in the main loop: 7
Мой вопрос заключается в том, почему нет побочного эффекта в очереди, которую я пытаюсь распечатать,Разве очередь не должна была быть пустой в оба раза, когда размер был распечатан?