普通数组:
#include <string> #include <iostream> #include<vector> using namespace std; template<class T> bool is_equal(T *a, int size_a,T * b, int size_b) { if (size_b !=size_a) return false; for (int i = 0; i < size_a; ++i) if (a[i] != b[i]) return false; return true; } int main() { int a[] = {1,2,3,4}; int b[] = {1,2,3,4}; cout << is_equal(a,sizeof(a)/sizeof(*a),b,sizeof(b)/sizeof(*b)); }
替换为vector:
#include <string> #include <iostream> #include<vector> using namespace std; template<class T> bool is_equal(vector<T> a,vector<T> b) { const int size = a.size(); if (size != b.size()) return false; for (int i = 0; i < size; ++i) if (a[i] != b[i]) return false; return true; } int main() { vector<int> a,b; int temp = 0; cout << "input array a: "; while (cin >> temp) a.push_back(temp); cout << "input array b: "; cin.clear(); cin.ignore(); while (cin >> temp) b.push_back(temp); cout << is_equal(a,b); }