|
| 1 | +--- |
| 2 | +Title: 'emplace()' |
| 3 | +Description: 'Constructs a new element at the end of the queue in-place using forwarded arguments.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Containers' |
| 9 | + - 'Methods' |
| 10 | + - 'Queues' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-c-plus-plus' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`emplace()`** method of the `std::queue<T,Container>` container adaptor constructs a new element directly in the underlying container at the back of the queue, forwarding the provided arguments to the constructor of `T`. Because the object is constructed in place, this can improve performance for types with expensive copy or move operations. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +queue.emplace(args...) |
| 22 | +``` |
| 23 | + |
| 24 | +**Parameters:** |
| 25 | + |
| 26 | +- `args...` (variadic template parameters): Arguments forwarded to construct the new element of type `T`. |
| 27 | + |
| 28 | +**Return value:** |
| 29 | + |
| 30 | +Since C++17: A reference to the inserted element (the value returned by the underlying container's `emplace_back` method). |
| 31 | + |
| 32 | +## Example 1: Enqueueing log entries into a queue |
| 33 | + |
| 34 | +In this example, log messages are constructed in place and enqueued for later processing: |
| 35 | + |
| 36 | +```cpp |
| 37 | +#include <iostream> |
| 38 | +#include <queue> |
| 39 | +#include <string> |
| 40 | + |
| 41 | +struct LogEntry { |
| 42 | + std::string level; |
| 43 | + std::string message; |
| 44 | + LogEntry(std::string lvl, std::string msg) |
| 45 | + : level(std::move(lvl)), message(std::move(msg)) {} |
| 46 | +}; |
| 47 | + |
| 48 | +int main(){ |
| 49 | + std::queue<LogEntry> logs; |
| 50 | + logs.emplace("INFO", "Application started"); |
| 51 | + logs.emplace("WARN", "Low disk space"); |
| 52 | + logs.emplace("ERROR", "Out of memory"); |
| 53 | + |
| 54 | + while(!logs.empty()){ |
| 55 | + const auto& entry = logs.front(); |
| 56 | + std::cout << "[" << entry.level << "] " << entry.message << "\n"; |
| 57 | + logs.pop(); |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | +
|
| 62 | +The output of this code is: |
| 63 | +
|
| 64 | +```shell |
| 65 | +[INFO] Application started |
| 66 | +[WARN] Low disk space |
| 67 | +[ERROR] Out of memory |
| 68 | +``` |
| 69 | + |
| 70 | +## Example 2: Constructing tasks in a task queue |
| 71 | + |
| 72 | +In this example, tasks with multiple constructor parameters are constructed directly inside the queue: |
| 73 | + |
| 74 | +```cpp |
| 75 | +#include <iostream> |
| 76 | +#include <queue> |
| 77 | +#include <functional> |
| 78 | + |
| 79 | +struct Task { |
| 80 | + int id; |
| 81 | + std::string description; |
| 82 | + Task(int i, std::string desc) |
| 83 | + : id(i), description(std::move(desc)) {} |
| 84 | + void run() const { std::cout << "Running task #" << id << ": " << description << "\n"; } |
| 85 | +}; |
| 86 | + |
| 87 | +int main(){ |
| 88 | + std::queue<Task> taskQueue; |
| 89 | + taskQueue.emplace(1, "Load configuration"); |
| 90 | + taskQueue.emplace(2, "Initialize modules"); |
| 91 | + taskQueue.emplace(3, "Start services"); |
| 92 | + |
| 93 | + while(!taskQueue.empty()){ |
| 94 | + taskQueue.front().run(); |
| 95 | + taskQueue.pop(); |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | +
|
| 100 | +The output of this code is: |
| 101 | +
|
| 102 | +```shell |
| 103 | +Running task #1: Load configuration |
| 104 | +Running task #2: Initialize modules |
| 105 | +Running task #3: Start services |
| 106 | +``` |
| 107 | + |
| 108 | +## Codebyte Example: Buffering sensor data with emplace |
| 109 | + |
| 110 | +In this example, sensor readings are constructed and enqueued as soon as they arrive, minimizing overhead: |
| 111 | + |
| 112 | +```codebyte/cpp |
| 113 | +#include <iostream> |
| 114 | +#include <queue> |
| 115 | +#include <tuple> |
| 116 | +
|
| 117 | +struct SensorData { |
| 118 | + int sensorId; |
| 119 | + double value; |
| 120 | + long timestamp; |
| 121 | + SensorData(int id, double val, long ts) |
| 122 | + : sensorId(id), value(val), timestamp(ts) {} |
| 123 | + void print() const { |
| 124 | + std::cout << "Sensor#" << sensorId |
| 125 | + << " value=" << value |
| 126 | + << " time=" << timestamp << "\n"; |
| 127 | + } |
| 128 | +}; |
| 129 | +
|
| 130 | +int main(){ |
| 131 | + std::queue<SensorData> buffer; |
| 132 | + buffer.emplace(101, 23.5, 1617181920L); |
| 133 | + buffer.emplace(102, 19.8, 1617181930L); |
| 134 | +
|
| 135 | + while(!buffer.empty()){ |
| 136 | + buffer.front().print(); |
| 137 | + buffer.pop(); |
| 138 | + } |
| 139 | +
|
| 140 | + return 0; |
| 141 | +} |
| 142 | +``` |
0 commit comments