emplace

Why can an aggreggate struct be brace-initialized, but not emplaced using the same list of arguments as in the brace initialization?

别来无恙 提交于 2020-01-01 04:48:10
问题 It seems like this code: #include <string> #include <vector> struct bla { std::string a; int b; }; int main() { std::vector<bla> v; v.emplace_back("string", 42); } could be made to work properly in this case, but it doesn't (and I understand why). Giving bla a constructor solves this, but removes the aggregateness of the type, which can have far-reaching consequences. Is this an oversight in the Standard? Or am I missing certain cases where this will blow up in my face, or is it just not as

When to use emplace* and when to use push/insert [duplicate]

浪子不回头ぞ 提交于 2019-12-23 02:56:36
问题 This question already has answers here : push_back vs emplace_back (7 answers) Closed 6 years ago . 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 ) emplace_back() and when to use push_back() and in general emplace* vs "old" insert functions? 回答1: emplace_back() only really makes sense when you

How to implement a simple container with placement new and emplace functionality?

元气小坏坏 提交于 2019-12-19 08:08:09
问题 I need to implement a container to hold an amount of elements and for some reason, it has to work without any heap allocation. Another requirement is, that the container elements should not be copied or moved in any way. They have to constructed directly into the memory allocated by the container. For that, I decided to use placement new and delegate the memory management completely to the container implementation (found some useful information about placement new at drdobbs). A running

Why emplace_back is faster than push_back?

£可爱£侵袭症+ 提交于 2019-12-17 15:37:08
问题 I thought that emplace_back would be the winner, when doing something like this: v.push_back(myClass(arg1, arg2)); because emplace_back would construct the object immediately in the vector, while push_back , would first construct an anonymous object and then would copy it to the vector. For more see this question. Google also gives this and this questions. I decided to compare them for a vector that would be filled by integers. Here is the experiment code: #include <iostream> #include <vector

insert vs emplace vs operator[] in c++ map

人走茶凉 提交于 2019-12-17 07:58:09
问题 I'm using maps for the first time and I realized that there are many ways to insert an element. You can use emplace() , operator[] or insert() , plus variants like using value_type or make_pair . While there is a lot of information about all of them and questions about particular cases, I still can't understand the big picture. So, my two questions are: What is the advantage of each one of them over the others? Was there any need for adding emplace to the standard? Is there anything that wasn

Inserting an object having a non copyable field into an std::vector

我与影子孤独终老i 提交于 2019-12-13 00:24:25
问题 I understand that the following code does not compile since the move constructor of A is deleted because the mutex is not movable. class A { public: A(int i) {} private: std::mutex m; }; int main() { std::vector<A> v; v.emplace_back(2); } But if I want my A to be stored in an std container how should I go about this? I am fine with A being constructed "inside" the container. 回答1: std::vector::emplace_back may need to grow a vector's capacity. Since all elements of a vector are contiguous,

std::map emplace gcc 4.8.2

大兔子大兔子 提交于 2019-12-10 15:46:08
问题 I am trying to use the emplace function of std::map, but it seems it is not implemented (but I read it was implemented in 4.8) The following code: std::map<std::string, double> maps; maps.emplace("Test", 1.0); leads to: class std::map<std::basic_string<char>, double>' has no member named 'emplace' Can someone clarify in which gcc version the emplace functions are implemented? 回答1: Here's some source code: #include <map> #include <string> int main() { std::map<std::string, double> maps; maps

emplace_back() vs push_back when inserting a pair into std::vector

守給你的承諾、 提交于 2019-12-07 13:34:40
问题 I defined the following std::vector<std::pair<int,int> > my_vec; my_vec.push_back( {1,2} ); //this works my_vec.emplace_back( {1,2} ); // this doesn't work std::pair<int,int> temp_pair = {1,2}; my_vec.emplace_back( temp_pair ); //this works I am compiling with c++11. The third line is problematic, but I thought you can use emplace_back() anywhere that you have push_back() , but this is apparently wrong. Why does the third line not work? 回答1: emplace_back takes a variadic parameter pack as

How to use emplace() in a std::map whose value is a std::set (map from something to a set)?

╄→гoц情女王★ 提交于 2019-12-06 11:17:24
The Problem I have a std::map<int, std::set<int>> named misi . I'm wondering why misi.emplace(2345, {6, 9}); and misi.emplace({2345, {6, 9}}); don't work as expected, as shown below. The Code #include <set> // std:set #include <map> // std::map #include <utility> // std::piecewise_construct, std::pair #include <tuple> // std::forward_as_tuple #include <iostream> // std::cout, std::endl int main() { // --- std::set initializer list constructor --- std::set<int> si({42, 16}); std::cout << "si.size(): " << si.size() << std::endl; // 2 std::cout << "*si.begin(): " << *si.begin() << std::endl; //

emplace_back() vs push_back when inserting a pair into std::vector

[亡魂溺海] 提交于 2019-12-06 02:50:19
I defined the following std::vector<std::pair<int,int> > my_vec; my_vec.push_back( {1,2} ); //this works my_vec.emplace_back( {1,2} ); // this doesn't work std::pair<int,int> temp_pair = {1,2}; my_vec.emplace_back( temp_pair ); //this works I am compiling with c++11. The third line is problematic, but I thought you can use emplace_back() anywhere that you have push_back() , but this is apparently wrong. Why does the third line not work? bolov emplace_back takes a variadic parameter pack as argument: template< class... Args > reference emplace_back( Args&&... args ); When you call it like this: