Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add to vector without copying struct data

Tags:

c++

vector

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

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

The struct and the tuple (another struct) are predefined by the architecture and given to me in my method; so I can't change them. I want to generate and insert a tuple into the originaly empty tuple_list.

The simple solution is have a method which allocates a new tuple object, fills in the tuple data, then call tuple_list.push_back() and pass in the allocated tuple. But this would require allocating a new tuple only to have the push_back method copy all of the contents of the (large) tuple struct into an already defined memory space of the vector. So I'm paying the expense of an allocation/delete as well as the lesser expense of copying the tuple contents into the vector to do it this way. It seems rather inefficent, and since this method would be in the critical path of the function I would prefer something faster (admitedly I doubt this method would be the bottle-neck, and I know early optimization == bad. However, I'm asking this question more to learn something about C++ syntax then out of a deperate need to actually do this in my code).

So my question is, is there a quicker way to fill the contents of my tuple list without allocating and copying a tuple? If this was an array I could make the array as large as I want, then past a reference to tuple_list[0] to the function that creates the tuple. That way the funciton could fill the empty contents of the already allocated tuple within the array without allocating a new one or copying from one tuple to another. I tried to do that with the vector out of curiousity and ended up with a seg fault when my itterator pointed to 0x0, so I assume that syntax doesn't work for vectors. So is there a quick way of doing this assignment?

Since this is a question as much to learn the language as for actual use feel free to throw in any other tangentally relevant stuff you think are interesting, I'm looking to learn.

Thanks.

like image 917
dsollen Avatar asked May 07 '12 20:05

dsollen


1 Answers

In C++11, you can use std::vector::emplace_back, which constructs the new object in-place, therefore there is no copying when you use this method.

By using this method, you could 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-allocated 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 a reference to the tuple after it has been stored 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();

On all of the above solutions you're creating only one tuple while also avoiding copying.

like image 170
mfontanini Avatar answered Sep 21 '22 04:09

mfontanini