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.
dsollen
source share