std::vector::emplace_back
Aus cppreference.com
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
template<class... Args> void emplace_back( Args&&... args); | (seit C++11) | |
Fügt ein neues Element am Ende des Behälters. Das Element in-place ist so aufgebaut, dh kein Kopieren oder Verschieben von Operationen durchgeführt werden. Der Konstruktor des Elements wird mit genau den gleichen Argumenten, die an die Funktion geliefert werden genannt .
Original:
Appends a new element to the end of the container. The element is constructed in-place, i.e. no copy or move operations are performed. The constructor of the element is called with exactly the same arguments that are supplied to the function.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
If the new size()
is greater than capacity()
, all iterators and references are invalidated. Otherwise no iterators and references are invalidated.
Inhaltsverzeichnis |
[Bearbeiten]Parameter
args | - | Argumente, die an den Konstruktor des Elements weiterzuleiten Original: arguments to forward to the constructor of the element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[Bearbeiten]Rückgabewert
(None)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten]Komplexität
Constant .
Original:
Constant.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten]Beispiel
Der folgende Code verwendet
emplace_back
ein Objekt vom Typ President
einer std::vector anhängen. Es zeigt, wie emplace_back
vorne Parameter an die President
Konstruktor und zeigt, wie mit emplace_back
vermeidet die zusätzlichen Kopieren oder Verschieben erforderlich, wenn push_back
. Original:
The following code uses
emplace_back
to append an object of type President
to a std::vector. It demonstrates how emplace_back
forwards parameters to the President
constructor and shows how using emplace_back
avoids the extra copy or move operation required when using push_back
. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <vector>#include <string>#include <iostream> struct President {std::string name;std::string country;int year; President(std::string&& p_name, std::string&& p_country, int p_year): name(std::move(p_name)), country(std::move(p_country)), year(p_year){std::cout<<"I am being constructed.\n";} President(President&& other): name(std::move(other.name)), country(std::move(other.country)), year(other.year){std::cout<<"I am being moved.\n";} President& operator=(const President& other)=default;}; int main(){std::vector<President> elections;std::cout<<"emplace_back:\n"; elections.emplace_back("Nelson Mandela", "South Africa", 1994); std::vector<President> reElections;std::cout<<"\npush_back:\n"; reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936)); std::cout<<"\nContents:\n";for(President const& president: elections){std::cout<< president.name<<" was elected president of "<< president.country<<" in "<< president.year<<".\n";}for(President const& president: reElections){std::cout<< president.name<<" was re-elected president of "<< president.country<<" in "<< president.year<<".\n";}}
Output:
emplace_back: I am being constructed. push_back: I am being constructed. I am being moved. Contents: Nelson Mandela was elected president of South Africa in 1994. Franklin Delano Roosevelt was re-elected president of the USA in 1936.
[Bearbeiten]Siehe auch
fügt Elemente am Ende Original: adds elements to the end The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (öffentliche Elementfunktion) |