Questions tagged [emplace]

For questions that concern constructing objects directly within some container object, as opposed to copying/moving the object into the container.

Use for questions related to the act of doing something in place, like for example inserting a new element in place.

For example, for an std::vector in , one could use std:emplace_back(), which is described like:

Inserts a new element at the end of the vector, right after its current last element. This new element is constructed in place using args as the arguments for its constructor.

183 questions
1
vote
2 answers

c++ unordered map emplace Invalid arguments

if I have this map std::unordered_map sockets; //a map holding all active sockets how come I can do this : sockets[_myId]=(int*)lp; //all ok - insert succeeds but I can't do this : if(!sockets.emplace(_myId,(int*)lp).second) {…
ZoOl007
  • 407
  • 3
  • 22
1
vote
3 answers

How does `emplace_back` in an `std::vector` work?

I noticed that emplace_back in an std::vector changes the address of previous vector elements. Why?
user3561614
  • 1,024
  • 1
  • 12
  • 20
1
vote
1 answer

Address of an instance emplaced to std::vector is invalid

I have 2 std::vectors: to first vector, I emplace instance to second vector, I want to store the address of the instance just emplaced But it does not work, i.e., the stored address differs from the emplaced instance's address. If it matters at…
lukes
  • 183
  • 1
  • 9
1
vote
1 answer

How to insert / emplace into map to avoid creation of temporary objects?

I'm reading a recommendation from one of C++11 books to prefer emplace over insert when adding items to the container in order to avoid creation of temporary objects (calls of constructors / destructors of the objects being inserted). But I'm a bit…
tommyk
  • 3,187
  • 7
  • 39
  • 61
1
vote
2 answers

How to emplace object without constructor arguments to STL constainer?

Suppose I have an class that has constructor taking no arguments, and an STL container of its objects: list lst; Is there a way to insert a new object in-place? I know something like lst.push_back(Object()); is going to work, equally fast as…
pkubik
  • 780
  • 6
  • 19
1
vote
1 answer

Copy from vector to vector, using C++11 features

I have a std::vector that I want to copy into a std::vector, in a performance-critical section. I've looked at solutions using std::copy(), boost::make_transform_iterator, std::vector newVec; newVec.reserve(oldVec.size()); ..…
Chap
  • 3,649
  • 2
  • 46
  • 84
1
vote
3 answers

Is there a way to emplace return value?

// VERSION 1 struct Range { int begin, end; }; inline Range getRange() { int newBegin, newEnd; // do calculations return {newBegin, newEnd}; } struct Test { std::vector ranges; inline void intensive() { …
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
1
vote
1 answer

When to use emplace* and when to use push/insert

I know of general idea of emplace functions on containers("construct new element inplace"). My question is not what it does, but more of like Effective C++11 one. What are good rules for deciding when to use (for eg when it comes to std::vector)…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
0
votes
1 answer

Construction of an object inside a vector with emplace_back wipes previous value attribute data in map of corresponding object pointers

I am writing a function that creates a vector of Product objects from .csv file data and emplaces the object pointer value into a map with its corresponding Product ID as the key. When I run a test that should print each objects attributes from the…
0
votes
2 answers

C++ vectors: emplace_back vs. push_back

Why is the method emplace_back() necessary at all? Why can compiler constructors not simply use the faster "create at end" way, when implementing push_back()? What part of the C++ standard prohibits that? What would be the problem in adjusting this…
Michael
  • 63
  • 5
0
votes
0 answers

Is emplace always efficient than insert when adding an element into a map?

In my thought, emplace is always a better option than insert, because in some case emplace could save one time of ctor and in other cases it could directly replace insert with the same invoking form. But now I have a map and add an element into it…
f1msch
  • 509
  • 2
  • 12
0
votes
1 answer

How to use try_emplace in a nested map to still meet the purpose what emplace achieve?

First I have codes like typedef struct st_A{ int a = 0; string s; st_A(const int a, const string&s) :a(a),s(s) {} }st_A; map m1; Now I want to insert a new element of pair{"c1", st_A{10, "c"}} into this map, so I code…
f1msch
  • 509
  • 2
  • 12
0
votes
1 answer

C++: emplace to std::unordered_map value containing std::mutex

Could you, please, help me to figure out the right syntax of how to emplace into an std::unordered_map a value containing an std::mutex? Here is an example: #include #include using SubMap = std::unordered_map
S.V
  • 2,149
  • 2
  • 18
  • 41
0
votes
1 answer

Force decay of string literals (const char array) to ptr

In the following example I try to emplace_back() a string literal and a string_view into an std::pair of string_views. However, my problem is that emplace_back() takes up the string literal as a const char array and doesn't decay it to a pointer.…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
2 answers

Does gcc have a extension overload for std::vector::emplace_back?

According to cppreference, std::vector::emplace_back has only one signature, which is: template< class... Args > reference emplace_back( Args&&... args ); And in the description, it says emplace_back is supposed to forward each of its arguments to…
palapapa
  • 573
  • 2
  • 5
  • 25