C++内置STL容器的深拷贝和浅拷贝(含代码验证)

目录

(头文件自行添加):结论以下6种STL容器全为深拷贝(有用请给个赞或者收藏)

1.string

2.vector

3.list

4.map

5.unordered_map

6. queue


(头文件自行添加):结论以下6种STL容器全为深拷贝(有用请给个赞或者收藏)
1.string

结论:深拷贝

template <typename T>
void func(T& a)
{
    T a1 = "456";
    cout << "func内: &a1=" << &a1 <<endl;
    cout << "a1=" << a1 <<endl;
    a = a1;
}

int main()
{
    string a = "123";
    cout <<"func前:&a=" << &a <<endl;
    cout << "a=" << a <<endl;
    func(a);
    cout <<"func后:&a="<< &a <<endl;
    cout <<"a="<< a <<endl;
}

func前: &a=0x7ffe48baa370
a=123
func内: &a1=0x7ffe48baa320
a1=456
func后: &a=0x7ffe48baa370
a=456 

2.vector

深拷贝

template <typename T>
void func(T& a)
{
    T a1 = {4,5,6};
    cout << "func内: &a1=" << &a1 <<endl;
    for(auto&num:a1)
        cout << "cc=" << num <<" ";
    cout <<endl;
    a = a1;
}
int main()
{
    vector<int> a = {1,2,3};
    cout <<"func前: &a=" << &a <<endl;
    for(auto&num:a)
        cout << "cc=" << num <<" ";
    cout <<endl;
    func(a);
    cout <<"func后: &a="<< &a <<endl;
    for(auto&num:a)
        cout << "cc=" << num <<" ";
    cout <<endl;
}

func前: &a=0x7ffd83849330
cc=1 cc=2 cc=3 
func内: &a1=0x7ffd838492a0
cc=4 cc=5 cc=6 
func后: &a=0x7ffd83849330
cc=4 cc=5 cc=6  

3.list

深拷贝

template <typename T>
void func(T& a)
{
    T a1 = {4,5,6};
    cout << "func内: &a1=" << &a1 <<endl;
    for(auto&num:a1)
        cout << "cc=" << num <<" ";
    cout <<endl;
    a = a1;
}
int main()
{
    list<int> a = {1,2,3};
    cout <<"func前: &a=" << &a <<endl;
    for(auto&num:a)
        cout << "cc=" << num <<" ";
    cout <<endl;
    func(a);
    cout <<"func后: &a="<< &a <<endl;
    for(auto&num:a)
        cout << "cc=" << num <<" ";
    cout <<endl;
}

func前: &a=0x7ffffedd7d10
cc=1 cc=2 cc=3 
func内: &a1=0x7ffffedd7c80
cc=4 cc=5 cc=6 
func后: &a=0x7ffffedd7d10
cc=4 cc=5 cc=6

4.map

 深拷贝

template <typename T>
void func(T& a)
{
    T a1 = {{4,"14"},{5,"15"},{6,"16"}};
    cout << "func内: &a1=" << &a1 <<endl;
    for(auto&cc:a1)
        cout << "cc.first=" << cc.first << "cc.cc.second=" << cc.second <<endl;
    cout <<endl;
    a = a1;
}

int main()
{
    map<int,string> a = {{1,"11"},{2,"12"},{3,"13"}};
    cout <<"func前: &a=" << &a <<endl;
    for(auto&cc:a)
        cout << "cc.first=" << cc.first << "cc.cc.second=" << cc.second <<endl;
    cout <<endl;
    func(a);
    cout <<"func后: &a="<< &a <<endl;
    for(auto&cc:a)
        cout << "cc.first=" << cc.first << "cc.cc.second=" << cc.second <<endl;
    cout <<endl;
}

func前: &a=0x7ffff21844d0
cc.first=1cc.cc.second=11
cc.first=2cc.cc.second=12
cc.first=3cc.cc.second=13

func内: &a1=0x7ffff21843b0
cc.first=4cc.cc.second=14
cc.first=5cc.cc.second=15
cc.first=6cc.cc.second=16

func后: &a=0x7ffff21844d0
cc.first=4cc.cc.second=14
cc.first=5cc.cc.second=15
cc.first=6cc.cc.second=16

5.unordered_map

深拷贝

template <typename T>
void func(T& a)
{
    T a1 = {{4,"14"},{5,"15"},{6,"16"}};
    cout << "func内: &a1=" << &a1 <<endl;
    for(auto&cc:a1)
        cout << "cc.first=" << cc.first << "cc.cc.second=" << cc.second <<endl;
    cout <<endl;
    a = a1;
}

int main()
{
    unordered_map<int,string> a = {{1,"11"},{2,"12"},{3,"13"}};
    cout <<"func前: &a=" << &a <<endl;
    for(auto&cc:a)
        cout << "cc.first=" << cc.first << "cc.cc.second=" << cc.second <<endl;
    cout <<endl;
    func(a);
    cout <<"func后: &a="<< &a <<endl;
    for(auto&cc:a)
        cout << "cc.first=" << cc.first << "cc.cc.second=" << cc.second <<endl;
    cout <<endl;
}

func前: &a=0x7ffedfb16e60
cc.first=3cc.cc.second=13
cc.first=2cc.cc.second=12
cc.first=1cc.cc.second=11

func内: &a1=0x7ffedfb16d30
cc.first=6cc.cc.second=16
cc.first=5cc.cc.second=15
cc.first=4cc.cc.second=14

func后: &a=0x7ffedfb16e60
cc.first=6cc.cc.second=16
cc.first=5cc.cc.second=15
cc.first=4cc.cc.second=14

6. queue

深拷贝

#include <iostream>
#include <queue>
#include <deque>
#include <list>
using namespace std;

class st_test       
{
public:
	int m_key;          
	string m_value;  
	st_test(const int& key, const string& value) : m_key(key), m_value(value) {}
};

void func(queue<st_test, list<st_test>>& q)
{
    queue<st_test, list<st_test>> q1;
    q1.push(st_test(4, "14"));  
	q1.push(st_test(5, "15"));
	q1.push(st_test(6, "16"));
    
    q = q1;

    cout << "func内: &q1=" << &q1 <<endl;
    while (q1.empty() == false)
	{
		cout << "key:" << q1.front().m_key << ",value:" << q1.front().m_value << endl;
		q1.pop();
	}
    cout << "func内: &q=" << &q <<endl;
    while (q.empty() == false)
	{
		cout << "key:" << q.front().m_key << ",value:" << q.front().m_value << endl;
		q.pop();
	}
}


int main()
{

    queue<st_test, list<st_test>> q;      
    q.push(st_test(1, "11"));  
	q.push(st_test(2, "12"));
	q.push(st_test(3, "13"));
    cout << "func前: " << "&q=" << &q <<endl;
    while (q.empty() == false)
	{
		cout << "key:" << q.front().m_key << ",value:" << q.front().m_value << endl;
		q.pop();
	}

    func(q);

    cout << "func后" <<endl;
    cout << "&q=" << &q <<endl;
    if(q.empty() == true)
        cout << "q is empty" <<endl;
}

func前: &q=0x7ffee34de590
key:1,value:11
key:2,value:12
key:3,value:13
func内: &q1=0x7ffee34de430
key:4,value:14
key:5,value:15
key:6,value:16
func内: &q=0x7ffee34de590
key:4,value:14
key:5,value:15
key:6,value:16
func后
&q=0x7ffee34de590
q is empty

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NaNbNa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值