第二十天第一个问题
问题描述:
应用STL中的list完成功能测试。
设计要求:
定义一个空的list,将用户输入的数组a[10]的10个数插入到list中,在list头部插入数b,用迭代器遍历list并输出其中的元素值。然后将list从大到小排序,删除list尾部的元素,用迭代器遍历list并输出其中的元素值。最后将list清空。
裁判测试程序样例:
#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
int main(){
int i,a[10],b;
for(i=0; i<10; i++){
scanf("%d",&a[i]);
}
scanf("%d",&b);//插入的数
{
/*请在这里填写答案*/
}
return 0;
}
输入样例:
10 1 2 3 4 5 6 7 8 9
0
输出样例:
[0][10][1][2][3][4][5][6][7][8][9]
[10][9][8][7][6][5][4][3][2][1]
代码:
list <int> x(a,a+10);
x.push_front(b);
list<int>::iterator iter;
for(iter = x.begin() ; iter != x.end() ; iter++)
{
cout<<"["<<*iter<<"]";
}
x.sort();
x.reverse();
x.pop_back();
cout<<endl;
for(iter = x.begin() ; iter != x.end() ; iter++)
{
cout<<"["<<*iter<<"]";
}
x.clear();

浙公网安备 33010602011771号