第二十天第一个问题

问题描述:

应用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();

posted @   序章0  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示