C ++ vector copy elements?

I would like to use a dynamic array in C ++ (something like ArrayList or Vector in Java.)
In this example, objects t1, t2 ... will be copied or is only its address added to the vector?
Do I need to implement a copy constructor for the Node class, or does the default constructor make a β€œcorrect” copy (because the class has a pointer)?
Or should I just declare vector<Node*> instead to avoid copying?
And do I need to implement a destructor to remove the other_node pointer other_node or can it be used by the program and still be stored in vector ?

 #include <vector> using namespace std; class Node { public: int id; Node* other_node; }; int main(int argc, char** argv) { vector<Node> nodes; Node t1; t1.id = 0; t1.other_node = NULL; Node t2; t2.id = 1; t2.other_node = &t1; Node t3; t3.id = 2; t3.other_node = &t2; Node t4; t4.id = 3; t4.other_node = &t1; nodes.push_back(t1); nodes.push_back(t2); nodes.push_back(t3); nodes.push_back(t4); for (vector<Node>::iterator it = nodes.begin(); it != nodes.end(); it++) { if (it->other_node) { printf("%d (other.id: %d)\n", it->id, it->other_node->id); } else { printf("%d (other.id: NULL)\n", it->id); } } getchar(); return 0; } 
+4
source share
2 answers

In your example vector<Node> copy of your nodes will be stored, so t1 , t2 will be copied.

In addition, the default copy constructor for Node will make a shallow copy. Thus,

 Node* head = new Node(); Node* next = new Node(); head->other_node = next; Node* other_head = new Node(*head); 

*(other_head->other_node) is the same Node as *(head->other_node) . It is up to you to decide whether this behavior suits you.

As for destructors: you should remove or free the memory that the instance of the class provided, unless you have good reason to own the memory. In the case of your list, in general, since your list did not allocate the memory specified in other_node , it should not be deleted.

Efficiency, since your Node is fairly inexpensive to copy (int and pointer), keeping the copy in order. If your Node class made a deep copy, then it would be better in terms of performance to use vector<Node*>

+4
source

std::vector , and other containers of the C ++ standard library have semantics of values, in other words, they expect that actual objects will not contain pointers to objects. Thus, whenever you place an object in a container of the standard library, the container copies it. Value semantics have certain consequences, such as automatically clearing a container from destruction, causing a memory leak if your container contains pointers to objects; in this particular case, you need to manually manually delete the objects with the pointer.

My recommendation would be that if you have objects that are cheap to copy or expensive to copy, but not often copied, put them in a container as a value. If you require the container to contain polymorphic objects or be copied frequently, dear to copy objects, hold them in the container either using boost::shared_ptr<> , or use the appropriate boost::ptr_xxx , for example, boost::ptr_vector .

+2
source

Source: https://habr.com/ru/post/1316721/


All Articles