other_节点){ printf(“%d(other.id:%d)\n”,it->id,it->other\u node->id); }否则{ printf(“%d(other.id:NULL)\n”,它->id); } } getchar(); 返回0; },c++,memory-management,dynamic-arrays,C++,Memory Management,Dynamic Arrays" /> other_节点){ printf(“%d(other.id:%d)\n”,it->id,it->other\u node->id); }否则{ printf(“%d(other.id:NULL)\n”,它->id); } } getchar(); 返回0; },c++,memory-management,dynamic-arrays,C++,Memory Management,Dynamic Arrays" />

C++;向量拷贝元素? 我想在C++中使用一个动态数组(比如数组或java中的向量)。 在这个例子中是t1,t2。。。复制对象或仅将其地址添加到向量? 我是否需要为节点类实现一个复制构造函数,或者默认构造函数是否会生成一个“正确”的副本(因为类中有一个指针)? 或者我应该声明一个向量而不是这个来避免复制吗? 我是否必须实现析构函数来删除其他节点指针,或者它是否被程序使用并仍然存储在向量中 #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; } #包括 使用名称空间std; 类节点{ 公众: int-id; 节点*其他_节点; }; int main(int argc,字符**argv){ 向量节点; 节点t1; t1.id=0; t1.other_node=NULL; 节点t2; t2.id=1; t2.其他_节点=&t1; 节点t3; t3.id=2; t3.其他节点=&t2; 节点t4; t4.id=3; t4.other_node=&t1; 节点。推回(t1); 节点。推回(t2); 节点。推回(t3); 节点。推回(t4); 对于(vector::iterator it=nodes.begin();it!=nodes.end();it++){ if(it->other_节点){ printf(“%d(other.id:%d)\n”,it->id,it->other\u node->id); }否则{ printf(“%d(other.id:NULL)\n”,它->id); } } getchar(); 返回0; }

C++;向量拷贝元素? 我想在C++中使用一个动态数组(比如数组或java中的向量)。 在这个例子中是t1,t2。。。复制对象或仅将其地址添加到向量? 我是否需要为节点类实现一个复制构造函数,或者默认构造函数是否会生成一个“正确”的副本(因为类中有一个指针)? 或者我应该声明一个向量而不是这个来避免复制吗? 我是否必须实现析构函数来删除其他节点指针,或者它是否被程序使用并仍然存储在向量中 #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; } #包括 使用名称空间std; 类节点{ 公众: int-id; 节点*其他_节点; }; int main(int argc,字符**argv){ 向量节点; 节点t1; t1.id=0; t1.other_node=NULL; 节点t2; t2.id=1; t2.其他_节点=&t1; 节点t3; t3.id=2; t3.其他节点=&t2; 节点t4; t4.id=3; t4.other_node=&t1; 节点。推回(t1); 节点。推回(t2); 节点。推回(t3); 节点。推回(t4); 对于(vector::iterator it=nodes.begin();it!=nodes.end();it++){ if(it->other_节点){ printf(“%d(other.id:%d)\n”,it->id,it->other\u node->id); }否则{ printf(“%d(other.id:NULL)\n”,它->id); } } getchar(); 返回0; },c++,memory-management,dynamic-arrays,C++,Memory Management,Dynamic Arrays,在您的示例中,vector将存储节点的副本,因此t1,t2将被复制 另外,节点的默认复制构造函数将生成“浅”复制。因此 Node* head = new Node(); Node* next = new Node(); head->other_node = next; Node* other_head = new Node(*head); *(其他头->其他节点)是与*(头->其他节点)相同的节点,由您决定这是否是您想要的行为 关于析构函数:您应该只删除/释放您的类实例分配的内存,除非您

在您的示例中,
vector
将存储节点的副本,因此
t1
t2
将被复制

另外,
节点
的默认复制构造函数将生成“浅”复制。因此

Node* head = new Node();
Node* next = new Node();
head->other_node = next;
Node* other_head = new Node(*head);
*(其他头->其他节点)
是与
*(头->其他节点)
相同的节点,由您决定这是否是您想要的行为

关于析构函数:您应该只删除/释放您的类实例分配的内存,除非您有充分的理由获得内存的所有权。就您的列表而言,一般来说,由于您的列表没有分配由
other_node
指向的内存,因此不应将其删除


就性能而言,由于您的节点复制成本相当低(一个int和一个指针),所以存储一个副本是可以的。如果你的节点类做了一个深层拷贝,那么从性能点来看,最好使用“代码>向量\代码/ < > P > >代码> STD::向量,而其他C++标准库容器有值语义,换句话说,它们希望保存实际对象而不是指向对象指针。因此,无论何时将对象放入标准库容器中,容器都会复制它。值语义具有一定的含义,例如,如果容器持有指向对象的指针,那么容器销毁时会自动清理,从而导致内存泄漏;在这种情况下,您需要自己手动删除指向的对象


我的建议是,如果您有复制成本低或复制成本高但不经常被复制的对象,请将它们作为值放入容器中。如果您需要容器保存多态对象或频繁复制、复制对象代价高昂的对象,请使用
boost::shared\u ptr
或使用适当的
boost::ptr\u xxx
容器将其保存在容器中,就像
boost::ptr\u vector

只是一个提示,因为您使用的是
std::vector
,您应该更喜欢
std::cout