C++ Primer(第5版) 练习 10.37
练习 10.37 给定一个包含10个元素的vector,将位置3到7之间的元素按逆序拷贝到一个list中。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp
/*************************************************************************
> File Name: ex10.37.cpp
> Author:
> Mail:
> Created Time: Thu 14 Mar 2024 03:38:34 PM CST
************************************************************************/
#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<iterator>
using namespace std;
int main(){
vector<int> number;
int num;
cout<<"Enter numbers: ";
while(cin>>num){
number.push_back(num);
if(cin.get() == '\n'){
break;
}
}
cout<<"Vector numbers: ";
for(auto n : number){
cout<<n<<" ";
}
cout<<endl;
list<int> lst;
for(auto iter = number.crbegin() + 4; iter != number.crend() - 3; ++iter){
lst.push_back(*iter);
}
cout<<"List numbers: ";
for(auto l : lst){
cout<<l<<" ";
}
cout<<endl;
return 0;
}