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<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;
}