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;
}
运行结果显示如下
相关推荐
1373i3 小时前
【Python】通俗理解反向传播
深度学习·算法·机器学习
Madison-No73 小时前
【C++】日期类运算符重载实战
c++·算法
椰子今天很可爱3 小时前
线程分离和线程同步互斥
linux·c++
cici158743 小时前
基于K-SVD的稀疏编码去噪算法
算法
电力程序小学童3 小时前
基于密集型复杂城市场景下求解无人机三维路径规划的Q-learning算法研究(matlab)
算法·matlab·无人机
lljss20203 小时前
C# 每个chartArea显示最小值、平均值、最大值
开发语言·c#
小柯J桑_3 小时前
Linux:线程控制
linux·c++·算法
1白天的黑夜13 小时前
栈-1047.删除字符串中的所有相邻重复项-力扣(LeetCode)
c++·leetcode·
yzpyzp3 小时前
kotlin的函数前面增加suspend关键字的作用
android·开发语言·kotlin
jiet_h3 小时前
Android Kotlin ObjectAnimator 和 ValueAnimator 全面解析
android·开发语言·kotlin