判断两个vector是否相等
Practice9.15
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <iterator>
#include <cmath>
#include <cstring>
#include <forward_list>
using namespace std;
bool my_equal(vector<int> v1, vector<int> v2) {
int len1 = (int)v1.size();
int len2 = (int)v2.size();
if(len1 != len2) return false;
for(int i = 0; i < len1; i++) {
if(v1[i] != v2[i]) return false;
}
return true;
}
int main() {
vector<int> v1 = {1, 2, 3, 4};
vector<int> v2 = {1, 2, 3, 4};
cout << "v1 == v2?" << endl;
cout << my_equal(v1, v2) << endl;
vector<int> v3 = {9, 8, 4, 4};
cout << "v1 == v3?" << endl;
cout << my_equal(v1, v3) << endl;
return 0;
}
Output
v1 == v2?
1
v1 == v3?
0
Practice9.16
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <iterator>
#include <cmath>
#include <cstring>
#include <forward_list>
using namespace std;
bool my_equal_2(list<int> l1, vector<int> v2) {
int len1 = (int)l1.size();
int len2 = (int)v2.size();
if(len1 != len2) return false;
auto it = l1.begin();
for(int i = 0; i < len2; i++) {
if(*it != v2[i]) return false;
it++;
}
return true;
}
bool my_equal(vector<int> v1, vector<int> v2) {
int len1 = (int)v1.size();
int len2 = (int)v2.size();
if(len1 != len2) return false;
for(int i = 0; i < len1; i++) {
if(v1[i] != v2[i]) return false;
}
return true;
}
int main() {
list<int> l1 = {1, 2, 3, 4};
vector<int> v1 = {1, 2, 3, 4};
vector<int> v2 = {8, 3, 45, 5};
cout << "l1 == v1?" << endl;
cout << my_equal_2(l1, v1) << endl;
cout << "l1 == v2?" << endl;
cout << my_equal_2(l1,v2) << endl;
return 0;
}
Output
l1 == v1?
1
l1 == v2?
0
Bad times make a good man