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

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

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

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
/*************************************************************************
	> File Name: ex9.16.cpp
	> Author: 
	> Mail: 
	> Created Time: Mon 26 Feb 2024 07:36:22 PM CST
 ************************************************************************/

#include<iostream>
#include<vector>
#include<list>
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 天前
【09】C语言中的格式输入函数scanf()详解
c语言·开发语言
初学小白...1 天前
实现Runnable接口
java·开发语言
Bruce-li__1 天前
CI/CD流水线全解析:从概念到实践,结合Python项目实战
开发语言·python·ci/cd
老王熬夜敲代码1 天前
ES安装和简单讲解
c++·微服务
Voyager_41 天前
算法学习记录08——并归的应用(LeetCode[315])
学习·算法·leetcode
是码农一枚1 天前
洞悉过往,一目了然:浅述视频融合平台EasyCVR如何实现海量视频录像的智能检索与高效回看
算法
是码农一枚1 天前
解密视频汇聚平台EasyCVR视频编解码与转码技术如何成就全协议、全终端的无缝视频体验
算法
yuzhuanhei1 天前
机器学习算法常用算法
人工智能·算法·机器学习
JustNow_Man1 天前
【Cline】插件中clinerules的实现逻辑分析
开发语言·前端·javascript
Bigger1 天前
🚀 真正实用的前端算法技巧:从 semver-compare 到智能版本排序
前端·javascript·算法