Mi chiedevo quali sono le differenze tra i due. Ho notato che emplace è l'aggiunta di C++ 11. Allora perché l'aggiunta?C++ std :: vector emplace vs insert
21
A
risposta
84
Emplace accetta gli argomenti necessari per costruire un oggetto sul posto, mentre insert prende (un riferimento a) un oggetto.
struct Foo
{
Foo(int n, double x);
};
std::vector<Foo> v;
v.emplace(someIterator, 42, 3.1416);
v.insert(someIterator, Foo(42, 3.1416));
21
insert
copie oggetti nel vettore.
emplace
costruire all'interno del vettore.
Problemi correlati
- 1. C++ push_back vs Insert vs emplace
- 2. C++: Stack's push() vs emplace()
- 3. std :: list vs std :: vector iteration
- 4. std :: vector vs normal array
- 5. sizeof() std :: vector (C++)
- 6. C++ std :: coppia, std :: vector e memcopy
- 7. C++ in uno std :: vector
- 8. std :: vector alternativa per C
- 9. Come funziona C++ std :: vector?
- 10. C++ Vector vs Array (Time)
- 11. std :: set non ha un membro emplace
- 12. std :: map :: emplace() mancante - librerie obsolete?
- 13. std :: vector :: cancellare vs "swap e pop"
- 14. funzioni make_shared e emplace
- 15. std :: map emplace senza valore di copia
- 16. vector :: al vs. vector :: operator []
- 17. Vector vs stringa
- 18. C++ 11 std :: vector in concurrent environment
- 19. C++ 11: std :: vector :: shrink_to_fit complex
- 20. Copia elementi da std :: vector in std :: stack C++
- 21. std :: vector <std::string> crash
- 22. Può std :: vector essere = 'd in un altro std :: vector?
- 23. std :: vector di std :: vettori contiguità
- 24. È std :: vector :: begin() da priorC++ 11 equivalente a std :: vector :: data() in C++ 11?
- 25. Vector clear vs. ridimensiona
- 26. Inizializzazione Eigen :: vector con std :: vector
- 27. std :: vector or boost :: vector thread safe?
- 28. Sparse Vector vs Dense Vector
- 29. Eigen e std :: vector
- 30. Ereditato da std :: vector
Si noti che in C++ 11 'insert' non è necessario copiare, ma può anche spostarsi. – juanchopanza
Vale la pena ricordare che mentre l'inserto può spostarsi se viene usato un cast di rvalue, potrebbe non farlo. Da qui la raccomandazione di Scott Meyer di utilizzare l'emplace quando è possibile per chiarezza delle prestazioni. – jeremyong