• C++ //练习 9.16 重写上一题的程序,比较一个list<int>中的元素和一个vector<int>中的元素。


    C++ Primer(第5版) 练习 9.16

    练习 9.16 重写上一题的程序,比较一个list中的元素和一个vector中的元素。

    环境:Linux Ubuntu(云服务器)
    工具:vim

     

    代码块
    /*************************************************************************
    	> File Name: ex9.16.cpp
    	> Author: 
    	> Mail: 
    	> Created Time: Mon 26 Feb 2024 07:36:22 PM CST
     ************************************************************************/
    
    #include
    #include
    #include
    using namespace std;
    
    int main(){
        vector<int> number;
        list<int> lst;
    
        int num;
        cout<<"Enter vector numbers: ";
        while(cin>>num){
            number.push_back(num);
            if(cin.get() == '\n'){
                break;
            }
        }
    
        cout<<"Enter list numbers: ";
        while(cin>>num){
            lst.push_back(num);
            if(cin.get() == '\n'){
                break;
            }
        }
    
        cout<<"Vector numbers: ";
        for(const auto n : number){
            cout<<n<<" ";
        }
        cout<<endl;
    
        cout<<"List numbers: ";
        for(const auto l : lst){
            cout<<l<<" ";
        }
        cout<<endl;
    
        list<int>::iterator li = lst.begin();
        int val = number[0];
        if(*li > val){
            cout<<*li<<" > "<<val<<endl;
        }
        else if(*li < val){
            cout<<*li<<" < "<<val<<endl;
        }
        else{
            cout<<*li<<" == "<<val<<endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    运行结果显示如下

    在这里插入图片描述

  • 相关阅读:
    高性能、低成本的高防IP产品现实吗?
    估计流量矩阵的方法
    ubuntu环境下openssl库的简单使用
    国内市场上的 BI 软件到底有啥区别?
    Kunyu安装使用教程(linux)
    Vue-vuex
    Linux查询服务器配置(CPU、内存RAM等)命令
    APT预警攻击平台截获Nday
    vue3+antdv 表格封装
    等保评测是什么意思
  • 原文地址:https://blog.csdn.net/navicheung/article/details/136307257