I am developing a memory critical application. I first generate a list, for example (C++11):
std::list<string> nodes({"Hello","Welcome", "Hi", "World"});
I need to now create a smaller list with the second and third elements of 'nodes.' Naively, I would do:
 std::list<string> sub_nodes;
 sub_nodes.push_back(*std::next(nodes.begin(),1));
 sub_nodes.push_back(*std::next(nodes.begin(),2));
But this clearly allocates memory in the heap for sub_nodes. What I wish to accomplish is to have the elements of sub_nodes occupy the same address of the elements of nodes from which they were created. In other words, I want the changes made to elements of sub_nodes reflected in those elements in nodes and vice versa. In C linked lists, this would be straight forward since the list nodes are basically pointers. How would I accomplish the same in C++?
Aucun commentaire:
Enregistrer un commentaire