Skip to main content
Filter by
Sorted by
Tagged with
0 votes
1 answer
103 views

I am trying to populate a std::vector container using another std::vector and tbb::parallel_for method as shown in the following code snippet: #include <tbb/parallel_for.h> #include <eigen3/...
abhi.goudar's user avatar
1 vote
0 answers
119 views

I thought the point of emplace_back was to forward arguments to the constructor instead of constructing a temporary object, but here it gives me a long nasty compiler error I can't interpret, whereas ...
H.v.M.'s user avatar
  • 1,746
0 votes
0 answers
91 views

I was trying to implement an emplace_back method for a statically-sized array (for fun). I came across the problem of whether I should use placement-new or move assignment for this emplace back method....
timmy george's user avatar
2 votes
2 answers
90 views

This compiles: #include <map> struct S { int val = 0; S() = default; S(S&&) = default; }; int main() { std::map<int, S> values; values.emplace(42, S{}); } ...
Oersted's user avatar
  • 3,904
2 votes
1 answer
122 views

With an std::vector I've managed to use emplace_back by forwarding the arguments, as in this code: #include<iostream> #include<map> #include <string> #include <utility> #...
roschach's user avatar
  • 9,646
2 votes
1 answer
91 views

I got a my implementation of STL queue data structure in C++ with a template class. I'm facing a problem that is implementation of emplace ( ) method for this template ADT. In emplace we use receiving ...
Muhammad Umer Farooq Bajwa's user avatar
3 votes
1 answer
115 views

#include <vector> #include <iostream> #include <algorithm> #include <string> #include <unordered_map> #include <unordered_set> using namespace std; // 双向链表 struct ...
Sprinining's user avatar
1 vote
0 answers
79 views

I am trying to understand the sample code at the emplace_hint() documentation: https://en.cppreference.com/w/cpp/container/map/emplace_hint Especially the following function: std::size_t ...
Jaanus Sepp's user avatar
1 vote
2 answers
94 views

Let's say I have a globally scoped set defined as follows: std::set<Person> people; I also have a function where I create an instance of the class Person, and add it to the set as follows: void ...
SimpleCoder's user avatar
  • 1,413
0 votes
0 answers
68 views

There is a code like this: using d2dArray = vector<vector<double>>; // Псевдоним d2dArray calcNextArray(d2dArray array) { d2dArray nextArray; int nextArrSize = array.size() - 1; ...
Gregor Bekk's user avatar
0 votes
1 answer
65 views

Suppose we have two structs: struct FIRST{ int a=0; int b=0; FIRST(int a, int b): a(a), b(b){} }; struct SECOND{ int c=0; int d=0; SECOND(int c, int d): c(c), d(d){} }; And we ...
MPEI_stud's user avatar
  • 135
1 vote
2 answers
2k views

I was trying to create objects for optional variables in-place. When using emplace function in std optional library for a class, the destructor of the class is called. Does that function do copy ...
green blanket's user avatar
1 vote
3 answers
832 views

This was a source of confusion for me, but std::unordered_map::try_emplace takes the key and variable arguments for the constructor of the value(mapped) type, however ::emplace is supposed to take an ...
Zebrafish's user avatar
  • 16.5k
0 votes
1 answer
125 views

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 ...
Chris Allison's user avatar
3 votes
1 answer
1k views

In the following code, I am trying to use aggregate initialization with emplace_back. The catch here is that emplace_back takes the constructor arguments and constructs the object directly in the ...
Yousef Irshaid's user avatar
-1 votes
1 answer
93 views

I am trying to find out why in the following example, when I disable copy-semantics but preserve move-semantics, emplacing into std::vector works in one case but does not work in another (when ...
Enmaniac's user avatar
-3 votes
1 answer
282 views

I read one answer about using std::vector::emplace_back on std::vector<std::unique_ptr<SomeClass>> and getting memory leaks when passing raw pointers (my_vec.emplace_back(new SomeClass())) ...
kiner_shah's user avatar
  • 4,833
2 votes
1 answer
818 views

I'm trying to run the following code on VS2022: #include <vector> #include <initializer_list> int main() { std::vector<std::vector<int>> vec; //auto list = (std::...
Yousef Irshaid's user avatar
6 votes
1 answer
1k views

The common advice is to prefer using std::map::try_emplace in preference to std::map::emplace in almost every instance. I wrote a simple test to trace object creation/copying/moving/destruction when ...
Alex O's user avatar
  • 1,976
1 vote
2 answers
2k views

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 ...
Michael's user avatar
  • 73
4 votes
1 answer
2k views

I was using an std::optional to store a struct, and I initially went with: std::optional<Point_t> optPosition; // declared somewhere as a class member optPosition.emplace(..., ..., ...); works ...
fab's user avatar
  • 65
2 votes
1 answer
120 views

This must be a stupid oversight on my part, but how can I add to the back of a std::forward_list? I know usually the forward_list implementation doesn't contain an iterator to the back, but according ...
glades's user avatar
  • 5,392
1 vote
1 answer
227 views

I implement my own vector class just to practice my C++ skill and my emplace() method is way slower than std::vector::emplace() so I checked the source code to see how they implemented it but it is ...
fettahyildiz's user avatar
0 votes
0 answers
52 views

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. ...
f1msch's user avatar
  • 665
0 votes
1 answer
550 views

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<string, st_A> m1; Now I want to insert a new ...
f1msch's user avatar
  • 665
2 votes
1 answer
515 views

In c++17 first, I have a struct like typedef struct ST_A{ int a; string s; ST_A(const int a, const string&s) :a(a),s(s) {} }st_A; And Now I have a simple map and emplace twice in ...
f1msch's user avatar
  • 665
0 votes
1 answer
260 views

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 <mutex> #include <...
S.V's user avatar
  • 2,855
0 votes
1 answer
166 views

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 ...
glades's user avatar
  • 5,392
2 votes
1 answer
230 views

The following code: std::pmr::vector<std::pmr::vector<int>> outer_vec(std::pmr::get_default_resource()); outer_vec.emplace_back(std::pmr::get_default_resource()); fails with quite ...
Zereges's user avatar
  • 5,219
0 votes
2 answers
119 views

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 ...
palapapa's user avatar
  • 1,051
1 vote
1 answer
112 views

I have an example_class with two constant fields. When I try to emplace an object of that class into a std::vector, I get various errors such as "error: object of type 'example_class' cannot be ...
Alexander Richter's user avatar
0 votes
1 answer
188 views

I have a trouble with adding a new values to a set<Value_Class> that is in my map<Key, set<Values_Class>>. When I try to add a value like this: Key key(get<0>(key_args), get<...
Artem's user avatar
  • 23
0 votes
1 answer
806 views

I have an issue with emplace and try_emplace as they always use the copy constructors when moving an object in. #include <iostream> #include <unordered_map> #include <map> using ...
Quang Le's user avatar
0 votes
2 answers
188 views

How can I write my own emplace method in my deque? For example, I have this emplace_front function template <typename ... Args> void emplace_front(Args&& ...val) { ??? } and Node ...
NJrslv's user avatar
  • 25
2 votes
2 answers
498 views

I am confused about std::maps's implementation of emplace(). emplace() is a variadic template function with the following declaration: template <class Args...> iterator emplace( Args&&......
chiasmos's user avatar
  • 140
3 votes
1 answer
1k views

One advantage of try_emplace member function of std::map (and std::unordered_map) is that it does not allocate a new node if the key already exists in the map. I wonder why this member function has ...
Daniel Langr's user avatar
  • 24.2k
0 votes
2 answers
82 views

Code below, for my linked list implementation and specifically emplace_back below. What am I doing wrong and how to fix? Error I am getting is: Build started... 1>------ Build started: Project: ...
Angus Comber's user avatar
  • 9,864
2 votes
2 answers
217 views

Here is what I am trying to do and what I have tried: #include <memory> #include <map> #include <string> using namespace std; struct MyStruct { const unique_ptr<int> a; ...
mqnc's user avatar
  • 766
1 vote
1 answer
237 views

I have an unordered map of string-pairs that i reference via a message id (key). Now I want to construct the string pair in place using the temporary string objects that the function receives, hence ...
glades's user avatar
  • 5,392
0 votes
1 answer
144 views

How to correct move resource from existed unique_ptr to another created in container? I want to put to container some unique_ptr with resource from another unique_ptr. Or maybe move one unique_ptr ...
Pasha's user avatar
  • 89
-1 votes
1 answer
2k views

I have a std::map<CompositeKey, std::string>, where CompositeKey is a class I wrote. This CompositeKey has three int data members, all the constructors, all the copy assignment operators and a ...
vaeVictis's user avatar
  • 502
1 vote
1 answer
341 views

I have a class that contains a std::mutex so it is not movable or copiable. struct MyObject { MyObject(std::string s_) : s(s_) {}; std::mutex lock; std::thread worker; std::string s; }; I can ...
TheBat's user avatar
  • 1,043
2 votes
1 answer
410 views

I don't understand when we can use const variables/objects in collections (particularly with emplace). The below code works with const objects directly but not when using pointers to const objects. #...
Verwirrt's user avatar
  • 413
1 vote
1 answer
217 views

How can we remove elements from a set like below which has iterator as part of its key? .erase() throws error saying no matching member function for call to 'erase'. Just like emplace() can be used ...
Bharat MV's user avatar
2 votes
0 answers
44 views

I was trying to use C++ std::vector's emplace_back (along with std::piecewise_construct and std::forward_as_tuple) on a vector of tuple, but compiler complain about template argument deduction/...
Mint's user avatar
  • 21
0 votes
1 answer
53 views

Code: std::map<CString, S_DISCUSSION_HIST_ITEM> mapHistory; // History list is in ascending date order for (auto& sHistItem : m_listDiscussionItemHist) { if (m_bFullHistoryMode) ...
Andrew Truckle's user avatar
0 votes
3 answers
799 views

I'm trying to avoid copying with emplace_back() and reserve(). But when I've tried to do it, i caught myself getting 3 copies for reason i cannot really understand. reserve() actually helps to avoid ...
Venci's user avatar
  • 25
0 votes
1 answer
1k views

To head this off, I've already read through the following question. My question could be seen as a follow up to this. insert vs emplace vs operator[] in c++ map From what info I can find, I don't see ...
TheChemist's user avatar
2 votes
2 answers
217 views

I'm trying to pass a type as a argument to a method that will properly construct and push a object to a vector of unique_ptr, however the created object is always the Base object. This happens when ...
Mete's user avatar
  • 313
0 votes
1 answer
155 views

I am trying to convert the following piece of code from C++ to C. I created an array, but I'm not sure what can be a precise replacement for emplace() and reserve() and for size_t, what can be done in ...
P Pp's user avatar
  • 279