list、vector、deque互相拷贝

时间:2022-02-21 18:48:08
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <list>
#include <deque>
using namespace std; //利用模版进行输出
template <typename Tv>
void print_v(vector<Tv> a)
{
//输出方法1
for(auto i:a)
{
cout<<i<<'\t';
}
cout<<endl;
//输出方法2
typename vector<Tv>::iterator it;//前面要加typename,要不会出错
for(it=a.begin();it!=a.end();it++)
{
cout<<*it<<'\t';
}
cout<<endl;
}
template <typename Tl>
void print_l(list<Tl> a)
{
//输出方法1
for(auto i:a)
{
cout<<i<<'\t';
}
cout<<endl;
//输出方法2
typename list<Tl>::iterator it;//前面要加typename,要不会出错
for(it=a.begin();it!=a.end();it++)
{
cout<<*it<<'\t';
}
cout<<endl;
}
template <typename Tq>
void print_q(deque<Tq> a)
{
//输出方法1
for(auto i:a)
{
cout<<i<<'\t';
}
cout<<endl;
//输出方法2
typename deque<Tq>::iterator it;//前面要加typename,要不会出错
for(it=a.begin();it!=a.end();it++)
{
cout<<*it<<'\t';
}
cout<<endl;
} int main()
{
//copy list to list
list<string> l={"","","",""};
list<string> l_1(l);//方法1
list<string> l_2;
copy(l.begin(), l.end(), back_inserter(l_2));//方法2
cout<<"l_1:\n";
print_l(l_1);
cout<<"l_2:\n";
print_l(l_2);
cout<<"----------------------------------------------"<<endl; //copy vector to vector
vector<string> v={"v1","v2","v3","v4"};
vector<string> v_1(v);//方法1
vector<string> v_2;
copy(v.begin(), v.end(), back_inserter(v_2));//方法2
cout<<"v_1:\n";
print_v(v_1);
cout<<"v_2:\n";
print_v(v_2);
cout<<"----------------------------------------------"<<endl; //copy list to vector
list<string> list1={"","","",""};
vector<string> vector1;
copy(list1.begin(), list1.end(), back_inserter(vector1));
cout<<"vector1:\n";
print_v(vector1);
cout<<"----------------------------------------------"<<endl; //copy vector to list
vector<string> vector2={"v1","v2","v3","v4"};
list<string> list2;
copy(vector2.begin(), vector2.end(), back_inserter(list2));
cout<<"list2:\n";
print_l(list2);
cout<<"----------------------------------------------"<<endl; //copy deque to vector
deque<string> deque3(,"abc");
vector<string> vector3;
copy(deque3.begin(), deque3.end(), back_inserter(vector3));
cout<<"vector3:\n";
print_v(vector3);
cout<<"----------------------------------------------"<<endl; //copy vector to deque
deque<string> deque4;
vector<string> vector4(,"dfg");
copy(vector4.begin(), vector4.end(), back_inserter(deque4));
cout<<"deque4:\n";
print_q(deque4);
cout<<"----------------------------------------------"<<endl; //copy deque to list
deque<string> deque5(,"dfg");
list<string> list5;
copy(deque5.begin(), deque5.end(), back_inserter(list5));
cout<<"list5:\n";
print_l(list5);
cout<<"----------------------------------------------"<<endl; //copy vector to deque
deque<string> deque6;
list<string> list6(,"dfg");
copy(list6.begin(), list6.end(), back_inserter(deque6));
cout<<"deque6:\n";
print_q(deque6);
cout<<"----------------------------------------------"<<endl;
return ;