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 天前
LeetCode 热题100-15.三数之和
数据结构·算法·leetcode
努力的小陈^O^1 天前
问题:Spring循环依赖问题排查与解决
java·开发语言·前端
Ccjf酷儿1 天前
C++语言程序设计 (郑莉)第十章 泛型程序设计与C++标准模板库
开发语言·c++
企业对冲系统官1 天前
基差风险管理系统日志分析功能的架构与实现
大数据·网络·数据库·算法·github·动态规划
ldccorpora1 天前
GALE Phase 1 Chinese Broadcast News Parallel Text - Part 1数据集介绍,官网编号LDC2007T23
人工智能·深度学习·算法·机器学习·自然语言处理
千金裘换酒1 天前
LeetCode 数组经典题刷题
算法·leetcode·职场和发展
FreeBuf_1 天前
利用零宽度字符的隐形JavaScript混淆工具InvisibleJS浮出水面
开发语言·javascript·ecmascript
lsx2024061 天前
Go 语言指针
开发语言
fie88891 天前
MATLAB有限元框架程序
python·算法·matlab
wearegogog1231 天前
基于MATLAB的IEEE 9节点系统潮流计算
开发语言·matlab