-3

This is what I've tried:

#include <iostream>
#include <list>
#include <vector>


template<typename T, typename U>
bool is_equal(T begin_vec,T end_vec, U begin_list, U end_list){
         while(begin_vec!=end_vec){
           
           if(*begin_vec==*begin_list){

             begin_vec++;
             begin_list++;
             return true;
             }


           else return false;
         }
     }

  int main(){
  std::vector<int> vec_num{1,2,3,4,5,6};
  std::list<int> list_num{1,2,3,4,5,6};
  if(is_equal(vec_num.begin(),vec_num.end(),list_num.begin(),list_num.end())){
    std::cout<<"They are equal!"<<std::endl;  
          }
       }

I think that a problem can arise if, for example, a vector has fewer elements and all elements are equal up to that point.

Xavi
  • 57
  • 8
  • 7
    Why not compare the container sizes first? – Nelfeal Dec 27 '22 at 17:20
  • 2
    You're not checking to see if you reach the end of the `list` which will results in UB if the list is shorter than the vector and if you fix the logic in the loop. – Captain Obvlious Dec 27 '22 at 17:23
  • 1
    why use two different container types? – Neil Butterworth Dec 27 '22 at 17:23
  • It's because this is homework and we must make a function with this signature: is_equal(vec_num.begin(),vec_num.end(),list_num.begin(),list_num.end()) – Xavi Dec 27 '22 at 17:26
  • 2
    `*end_list` exhibits undefined behavior right off the bat. – Igor Tandetnik Dec 27 '22 at 17:27
  • Sorry I meant *begin_list – Xavi Dec 27 '22 at 17:30
  • What does "efficiently compare" mean? – Sam Varshavchik Dec 27 '22 at 17:31
  • 1
    "If the first element of the vector is the same as the first element of the list, increment the begin iterators and then return `true`, because the collections must be equal". Doesn't seem quite right. – molbdnilo Dec 27 '22 at 17:31
  • 1
    Compare [`std::distance`](https://en.cppreference.com/w/cpp/iterator/distance) before comparing the elements. – HolyBlackCat Dec 27 '22 at 17:41
  • 1
    You know that there is a `std::equal` function with this exact signature, right? – BoP Dec 27 '22 at 17:54
  • Yes but we are not allowed to use it.. – Xavi Dec 27 '22 at 18:45
  • 1
    Before going for efficiency, you should go for correctness. Your code fails many test cases, more than just the problematic case you identified. ([Enable warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) to get a hint at another case that fails.) Sorry, I see too many errors for this to be suitable as a Stack Overflow question. – JaMiT Dec 27 '22 at 20:00

1 Answers1

-1
#include <iostream>
#include <list>
#include <vector>
#include <iterator>
    
   

template <typename T,typename U>
  bool is_equal  ( T beg1, T end1, U beg2, U end2 )
{ 
  if(std::distance(beg1,end1)!=std::distance(beg2,end2)){

    return false;
  }

  else{
  while (beg1!=end1) {
    if (!(*beg1 == *beg2)) {  
      return false;}
    ++beg1;
    ++beg2;
  }
     return true; 
       }
     }

    
    
    int main(){
      std::vector<int> vec_num{1,2,3,4,5,6};
      std::list<int> list_num{1,2,3,4,5,6};
      if(is_equal(vec_num.begin(),vec_num.end(),list_num.begin(),list_num.end())){
        std::cout<<"They are equal!"<<std::endl;  
       } 
    }
Xavi
  • 57
  • 8