We have two vectors of size that depends on runtime and need to check if they are equal - differ elements only after the end of smaller size vector. I used std::equal but the issue is that I need to find first which vector is of smaller size which leads to extra line of code :
#include <vector>
#include <iostream>
int main()
{
std::vector<int> a(1000, 3);
std::vector<int> a1(100, 3);
if(a.size() > a1.size())
{
if(std::equal(a1.begin(), a1.end(), a.begin()))
{
std::cout << "Same a gt a1" << std::endl;
}
}
if(a1.size() > a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a1 gt a" << std::endl;
}
}
if(a1.size() == a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a = a1" << std::endl;
}
}
}
Can the code to compare two vectors or differ only at the end of smaller vector be improved?
Aucun commentaire:
Enregistrer un commentaire