#include <iostream>
#include <vector>
#include <list>
using namespace std;
bool my_equal(list<int> &l, vector<int> &v)
{
//先判断容器大小是否相等,若不等,直接返回false
if(l.size() != v.size())
{
return false;
}
//如果容器大小相等,则按元素判断
auto itOfList = l.begin();
auto itOfVec = v.begin();
for( ;itOfList != l.end() && itOfVec != v.end();itOfList++,itOfVec++)
{
if(*itOfList != *itOfVec)
{
return false;
}
}
return true;
}
int main()
{
list<int> l1{1,2,3,4,5};
vector<int> v1{1,2,3,4,5};
vector<int> v2{1,2,3,4};
vector<int> v3{1,2,3,4,4};
cout << "l1 ? v1 " ;
cout << my_equal(l1,v1) << endl;
cout << "l1 ? v2 " ;
cout << my_equal(l1,v2) << endl;
cout << "l1 ? v3 " ;
cout << my_equal(l1,v3) << endl;
return 0;
}
输出结果:
l1 ? v1 1
l1 ? v2 0
l1 ? v3 0