C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Queue emplace() FunctionC++ Queue emplace() function adds a new element at the end of the queue, following the current back element. The function performs the insertion operation on the queue. Syntaxtemplate <class... Args> void emplace (Args&&... args); Parametersargs: The parameter forwards the argument for the construction of a new element. It specifies the value of the newly constructed element, which is to be inserted at the end position. Return valueThe function is used only for the addition of new elements and does not return any value. Example 1#include<iostream> #include<queue> #include<string> int main() { std::queue<std::string> newqueue; newqueue.emplace("I am the first line"); newqueue.emplace("I am the second one"); std::cout << "Contents of new queue: \n"; while (!newqueue.empty()) { std::cout << newqueue.front() << "\n"; newqueue.pop (); } return 0; } Output: I am the first line I am the second one Example 2#include<iostream> #include<queue> #include<string> using namespace std; int main() { queue<string> newpqueue; newpqueue.emplace("portal"); newpqueue.emplace("computer science"); newpqueue.emplace("is a"); newpqueue.emplace("TheDeveloperBlog"); cout << "newpqueue = " ; while(!newpqueue.empty( ) ) { cout<< newpqueue.front() << " "; newpqueue.pop(); } return 0 ; } Output: TheDeveloperBlog is a computer science portal ComplexityOne call is made to the emplace_back. Data racesAll the elements present in the queue are modified, as with the addition of a new element the respective positions of all the other elements are also changed. Exception SafetyGuarantee as equivalent to the operations that are performed on the underlying container object is provided.
Next TopicC++ Queue
|