C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Queue push() FunctionC++ Queue push() function is used for adding new elements at the rear of the queue. The function is implied for performing the insertion related operations. Syntaxvoid push (const value_type& value); Parametersvalue: The parameter represents the value to which the element is initialized. That is the value of the newly added element in the queue. Return valueThe function has no return type, and it only adds a new element to the queue. Example 1#include <iostream> #include <queue> int main() { std::queue<int> newqueue; int qint; std::cout << "Enter some valid integer values(press 0 to exit)"; do { std::cin>> qint; newqueue.push(qint); } while (qint); std::cout<< "newqueue contains: "; while(!newqueue.empty()) { std::cout <<" " <<newqueue.front(); newqueue.pop(); } return 0; } Output: Enter some valid integer values(press 0 to exit) 1 2 3 5 6 7 0 newqueue contains: 1 2 3 5 6 7 0 Example 2#include <iostream> #include <queue> using namespace std; int main() { queue<int> newqueue; newqueue.push(34); newqueue.push(68); while(!newqueue.empty()) { cout<<" "<<newqueue.front(); newqueue.pop(); } } Output: 34 68 ComplexityOne call is made to the pushback on the container that is underlying. Data racesThe modification is made to the container, and the elements contained. Exception SafetyGuarantee as equivalent to the operations that are performed on the underlying container object is provided.
Next TopicC++ Queue
|