Probably this is rather a simple question but I cannot wrap around why the result is what it is. Consider the following simple code.
#include <iostream>
#include <queue>
void print_queue(std::queue<int> q)
{
while( !queue.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;
}
The expected output of this will be:
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
My question is why there is no side effect on the queue I am trying to print out. Wasn't the queue supposed to be empty at both times when the size was printed out?
Aucun commentaire:
Enregistrer un commentaire