美文网首页
c++深度拷贝vector

c++深度拷贝vector

作者: 一路向后 | 来源:发表于2021-07-11 22:48 被阅读0次

1.头文件

#include <iostream>
#include <vector>

class A {
public:
        A(int r);
        A(const A &a);
        ~A();
        void print(A &a);
        void print();
        int x;
        std::vector<A> childs;
};

2.cpp文件

#include "test.h"

using namespace std;

A::A(int r)
{
    x = r;
}

A::A(const A &a)
{
    x = a.x;
    childs = a.childs;
}

A::~A()
{

}

void A::print(A &a)
{
    vector<A>::iterator it;

    cout << "x = " << a.x << endl;

    for(it=a.childs.begin(); it!=a.childs.end(); it++)
    {
        print((*it));
    }
}

void A::print()
{
    print(*this);
}

int main()
{
    A a(10);

    a.childs.push_back(A(11));
    a.childs.push_back(A(12));
    a.childs[0].childs.push_back(A(13));
    a.childs[1].childs.push_back(A(14));

    a.print();

    A b(a);

    cout << "---------------------------------" << endl;

    b.print();

    return 0;
}

3.编译源码

$ g++ -o test test.cpp

4.运行及其结果

$ /test
x = 10
x = 11
x = 13
x = 12
x = 14
---------------------------------
x = 10
x = 11
x = 13
x = 12
x = 14

相关文章

  • c++深度拷贝vector

    1.头文件 2.cpp文件 3.编译源码 4.运行及其结果

  • C++ vector拷贝与引用

    vector的拷贝与引用与普通的变量相似,实例如下: //拷贝 vector adder_cp(vector ...

  • vector

    vector 类型的数据,拷贝时,只有里面的 vector 内存空间是连续的,外层的 vect...

  • 2_11基数排序

    C++的queue实现 C++ vector 实现 python 实现

  • C++ STL 之 vectot(三)

    今天我们继续更新 C++ STL 中 vector 容器的使用 vector 容器增加元素 vector 容器增加...

  • OJ刷题知识点

    C++ | vector vector:向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence ...

  • 顺序容器vector

    转自C++ vector的用法(整理)#include 一、vector初始化的五种方式 二、v...

  • C++ 拷贝控制(二) — 移动构造函数和移动赋值运算符

    相关文章: C++ 拷贝控制(一) — 析构函数、拷贝构造函数与拷贝赋值函数 C++ 引用类型 — 左值引用、常引...

  • 深拷贝、浅拷贝

    父类实现深拷贝时,子类如何实现深度拷贝。父类没有实现深拷贝时,子类如何实现深度拷贝。 深拷贝同浅拷贝的区别:浅拷贝...

  • 面试题整理

    父类实现深拷贝时,子类如何实现深度拷贝。父类没有实现深拷贝时,子类如何实现深度拷贝。 深拷贝同浅拷贝的区别:浅拷贝...

网友评论

      本文标题:c++深度拷贝vector

      本文链接:https://www.haomeiwen.com/subject/wmplpltx.html