Add structure data to vector without copying data

This is supposedly a simple question in C ++, but I am relearning C ++ and don't know some basics. I have a class that includes a structure with a vector of objects in it, something like this:

struct my_struct{ Irrelevant_Object object, vector<tuple> tuple_list; } 

The structure and tuple (another structure) are predetermined by the architecture and provided to me in my method; therefore, I cannot change them. I want to generate and insert a tuple into the original empty tuple_list.

A simple solution has a method that selects a new tuple object, populates the tuple data, then calls tuple_list.push_back () and passes in the selected tuple. But this would require the allocation of a new tuple only for the push_back method to copy the entire contents of the (large) tuple structure into the already defined vector memory space. Therefore, I pay the costs of allocation / deletion, as well as lower costs for copying the contents of the tuple into a vector to do it this way. This seems rather inefficient, and since this method will be on the critical path of the function, I would prefer something faster (I am perplexed that this method will be a bottleneck, and I know the early optimization == bad. Asking this question more, to learn something about C ++ syntax, and then because of the delayed need to do this in my code).

So my question is: is there a faster way to populate the contents of the list of tuples without selecting and copying the tuple? If it were an array, I could make the array as large as I want, and then pass the link to tuple_list [0] to the function that creates the tuple. Thus, funciton can fill in the empty contents of an already selected tuple inside an array without allocating a new one or copying from one tuple to another. I tried to do this with a vector out of curiosity and ended up with a seg error when my iterator pointed to 0x0, so I assume the syntax does not work for vectors. So, is there a quick way to complete this task?

Since this is a question, there is so much to learn the language, because for actual use do not hesitate to drop any other related materials that you think are interesting, I am looking to learn.

Thanks.

+7
source share
1 answer

In C ++ 11, you can use std::vector::emplace_back , which creates a new object in place, so copying fails when using this method.

Using this method, you can do this:

 my_struct some_struct; some_struct.tuple_list.emplace_back(1, 5, "bleh"); 

Assuming your tuple object contains this constructor:

 tuple::tuple(int, int, const std::string&) 

Edit : you can also use move semantics to store a pre-selected tuple:

 my_struct some_struct; tuple a_tuple; /* modify a_tuple, initialize it, whatever... */ some_struct.push_back(std::move(a_tuple)); // move it into your vector 

Or use the link to tuple after it has been saved in the vector:

 my_struct some_struct; some_struct.tuple_list.emplace_back(1, 5, "bleh"); // store a reference to the last element(the one we've just inserted) tuple &some_tuple = some_struct.tuple_list.back(); some_tuple.foo(); 

In all of the above solutions, you only create one tuple and also avoid copying.

+15
source

All Articles