实验一
问题回答
1.reverse 直接在原容器中反转 reverse_copy()复制后反转给到新容器
2.rotate通过挪动元素,(a,b,c)a是起始,c是结束,从b新节点开始把后边的挪到前边
代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
template<typename T>
void output(const T &c);
void test1();
void test2();
void test3();
int main(){
std::cout<<"测试1:\n";
test1();
std::cout<<"\n测试2:\n";
test2();
std::cout<<"\n测试3:\n";
test3();
}
template <typename T>
void output(const T &c){
for(auto &i : c)
std::cout<<i<<' ';
std::cout<<'\n';
}
void test1(){
using namespace std;
string s0{"0123456789"};
cout<<"s0="<<s0<<endl;
string s1(s0);
reverse(s1.begin(),s1.end());
cout<<"s1="<<s1<<endl;
string s2(s0.size(),' ');
reverse_copy(s0.begin(),s0.end(),s2.begin());
cout<<"s2="<<s2<<endl;
}
void test2(){
using namespace std;
vector<int> v0{2,0,4,9};
cout<<"v0:";output(v0);
vector<int> v1{v0};
reverse(v1.begin(),v1.end());
cout<<"v1: ";output(v1);
vector<int> v2{v0};
reverse_copy(v0.begin(),v0.end(),v2.begin());
cout<<"v2:";output(v2);
}
void test3(){
using namespace std;
vector<int> v0{0,1,2,3,4,5,6,7,8,9};
cout<<"v0: ";output(v0);
vector<int> v1{v0};
rotate(v1.begin(),v1.begin()+1,v1.end());
cout<<"v1: ";output(v1);
vector<int> v2{v0};
rotate(v2.begin(),v2.begin()+2,v2.end());
cout<<"v2: ";output(v2);
vector<int> v3{v0};
rotate(v3.begin(),v3.end()-1,v3.end());
cout<<"v3: ";output(v3);
vector<int> v4{v0};
rotate(v4.begin(),v4.end()-2,v4.end());
cout<<"v4: ";output(v4);
}

浙公网安备 33010602011771号