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;
}
运行结果显示如下
相关推荐
lclin_20204 分钟前
大恒Galaxy SDK MFC开发(1):枚举、打开、开始_停止采集基础流程(无画面版)
c++·机器视觉·工业相机·大恒相机·galaxysdk
老约家的可汗12 分钟前
搜索二叉树的概念及使用
java·开发语言
初圣魔门首席弟子13 分钟前
bug20260415
c++·bug
不知名的老吴14 分钟前
R语言4.3.0安装包百度网盘中文版下载与详细安装指南
开发语言·r语言
棉猴16 分钟前
python海龟绘图之计算夹角towards()
开发语言·python·turtle·海龟绘图·towards
luoganttcc19 分钟前
CUDA grid/block 到矩阵映射示例(矩阵加法)
人工智能·算法·机器学习
张人玉21 分钟前
SMT 贴片机上位机项目
开发语言·c#
我不是懒洋洋21 分钟前
【经典题目】链表OJ(相交链表、环形链表、环形链表II、随机链表的复制)
c语言·开发语言·数据结构·链表·ecmascript·visual studio
ん贤23 分钟前
口述Map
开发语言·面试·golang
YuanDaima204824 分钟前
Python 数据结构与语法速查笔记
开发语言·数据结构·人工智能·python·算法