C++ set emplace() Function
Example
Add a new element to a set:
set<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars.emplace("Audi");
for (string car : cars) {
cout << car << "\n";
}
Definition and Usage
The emplace() function adds a new element to a set. It builds the element directly inside the set.
If the value already exists, nothing is added. The new element is placed in the correct sorted position.
The emplace function is defined in the <set> header file.
Tip: For simple values like strings and numbers, emplace() and insert() give the same result.
Syntax
set.emplace(type value)
type is the type of the elements in the set, like string in the example.
Parameter Values
| Parameter | Description |
|---|---|
| value | Required. The value of the new element. |
Technical Details
| Returns: | A pair. Its first is an iterator to the element, and its second is true if the element was added, or false if it already existed. |
|---|
Related Pages
Read more about sets in our Sets Tutorial.