C++ Primer(第5版) 练习 3.36
练习 3.36 编写一段程序,比较两个数组是否相等。再写一段程序,比较两个vector对象是否相等。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************
> File Name: ex3.36.cpp
> Author:
> Mail:
> Created Time: Thu 01 Feb 2024 02:17:27 PM CST
************************************************************************/
#include<iostream>
#include<vector>
using namespace std;
int main(){
int arr1[5], arr2[5];
cout<<"Enter arr1 5 numbers: ";
for(int i = 0; i < 5; i++){
cin>>arr1[i];
}
cout<<"Enter arr2 5 numbers: ";
for(int i = 0; i < 5; i++){
cin>>arr2[i];
}
int sign = 0;
for(int i = 0; i < 5; i++){
if(arr1[i] != arr2[i]){
sign = 1;
break;
}
}
if(sign == 0){
cout<<"arr1 is equal to arr2."<<endl;
}
else{
cout<<"arr1 is not equal to arr2."<<endl;
}
vector<int> arr3, arr4;
int num;
cout<<"Enter arr3 5 numbers: ";
for(int i = 0; i < 5; i++){
cin>>num;
arr3.push_back(num);
}
cout<<"Enter arr4 5 numbers: ";
for(int i = 0; i < 5; i++){
cin>>num;
arr4.push_back(num);
}
if(arr3 == arr4){
cout<<"arr3 is equal to arr4."<<endl;
}
else{
cout<<"arr3 is not euqal to arr4."<<endl;
}
return 0;
}