目录
(头文件自行添加):结论以下6种STL容器全为深拷贝(有用请给个赞或者收藏)
(头文件自行添加):结论以下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=13func内: &a1=0x7ffff21843b0
cc.first=4cc.cc.second=14
cc.first=5cc.cc.second=15
cc.first=6cc.cc.second=16func后: &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=11func内: &a1=0x7ffedfb16d30
cc.first=6cc.cc.second=16
cc.first=5cc.cc.second=15
cc.first=4cc.cc.second=14func后: &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