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
0
votes
1 answer

In protocol buffers how to `placement new` inside an arena?

Basically any object can be created on an arena with google::protobuf::Arena::Create. How is it possible to allocate another object in place of an already allocated one? I tried the following: class My_stuff{}; int main(){ google::protobuf::Arena…
Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46
0
votes
0 answers

How to insert an object into a map so the map takes ownership? emplace?

I want a map to hold an object and be responsible for its deletion either when the map comes out of scope or explicitly when 'erase' is called for one of its member objects. using std::court; struct A { int x; char y; A(int z,char c):…
user2304458
  • 363
  • 1
  • 2
  • 11
0
votes
0 answers

std::map emplace with key obtained during value construction

Suppose we have a map of some complex objects, textures in my case. std::map _table; What would be the most correct way to insert an object into such a map with a key which is obtained during the value (Texture)…
evovch
  • 43
  • 7
0
votes
1 answer

Performance comparison between three ways to read and store an array of objects

I'm trying to improve my coding skills in problem-solving using C++. Hence, I'm curious to know which one of the following three suggested examples is faster and better to use, as well as the best way to evaluate the performance. #include…
catfour
  • 119
  • 3
  • 10
0
votes
1 answer

C++ Emplace Back vs Push Back Vector

I am trying to understand emplace_back vs. push_back in Vector C++. Though the whole process of both emplace_back and push_back is to append data at the end, From what I understand from emplace, it has no temporary objection creation. But When I see…
Jagdish
  • 160
  • 3
  • 13
0
votes
1 answer

emplace_back initialisation list error, when initialisation list works on standalone variable

So I have been making a object pool class, it is used like so: class MagicTrick { public: MagicTrick(int magic) : _magic(magic) {} int predict() { return _number * _magic; } private: int _magic; int _number =…
Thomas Monkman
  • 335
  • 2
  • 10
0
votes
1 answer

add random element of a set to a list and remove it from original set

I have a set of strings and want to add random elements of that set to a list (e.g. distribute poker cards to different players) I've tried the following: std::set remaining_cards; std::vector> player_cards; int…
Nickpick
  • 6,163
  • 16
  • 65
  • 116
0
votes
2 answers

Why a vector constructor takes only last value of the parameter when using in unordered_map emplace?

I'm trying to implement unordered_map> with using unordered_map::emplace #include #include #include using namespace std; int main () { unordered_map> amap; amap.emplace('k',(2,9)); …
0
votes
0 answers

error: no member named 'emplace' , when compiling Liquibook using make all

In file included from ./Market.h:6: In file included from /Users/shubhamsharma/projects/liquibook/src/book/depth_order_book.h:6: /Users/shubhamsharma/projects/liquibook/src/book/order_book.h:559:17: error: no member named 'emplace' in …
7ur7l3
  • 31
  • 6
0
votes
2 answers

C++11 - emplacing a variable to any std container (vector, list, set, unordered_set)

Trying to write a generic code to emplace a default constructed object into an std container and return an iterator to it. with insert the solution is template typename C::iterator insert(C& container) { return…
Shloim
  • 5,281
  • 21
  • 36
0
votes
0 answers

Invoking the emplace() method for std::vector

What is the correct way to invoke the emplace() method for std::vector, given the following template declarations? The requirement is to emplace an element inside the vector impl_tree at a given position, after the construction of the tree class…
user9196120
  • 381
  • 3
  • 9
0
votes
1 answer

memory usage by a process in c++ program

In My C++ program I use * m_Map= new map >(); delete(m_Map); m_Map->erase(TxId); I added 1000000 elements to map and I checked time to time in the loop the memory usage of the process for(int x=1;x<=1000000;x++){ …
Noah
  • 55
  • 7
0
votes
1 answer

Efficient copying/casting of large matrix std::vector> and armadillo arma::mat

Is there an efficient copying mechanism between std::vector> std_mat arma::mat arma_mat where arma::mat arma_mat refers to armadillo matrix/math library. http://arma.sourceforge.net/ My project is…
Pal
  • 989
  • 10
  • 23
0
votes
1 answer

With CUDA 8 + gcc 5, unordered_map emplace with string key fails to compile

Consider the following code: #include #include int main() { std::string s("hello"); std::unordered_map map; map.emplace(s, 123); return 0; } This builds fine with: GCC 4.9.3 , no CUDA GCC…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
0
votes
2 answers

Can I get an istream to emplace rather than assign?

The istream operator>>'s require a reference to an existing object. That means what they do is assignment rather than in-place construction, right? Well, how can I skip the initial construction? Maybe the compiler can optimize it away, but I may…
einpoklum
  • 118,144
  • 57
  • 340
  • 684