Hi,

I wish to compare partial bits of two one dimensional vectors. I know how to compare the two vectors completely.

int main () {
  
  std::vector<int> vector1;
  std::vector<int> vector2;
  
  int out;
  vector1.push_back(100);
  vector2.push_back(101);
  
  vector1 == vector2 ? out = 0 : out = 1;
  
  printf("out = %d \n", out);
  return 0;
}

But i do not know how to compare partial bits.

For example:

Vector 1 = 0100
vector2 = 100

i wish to find whether vector 1 and vector 2 are equal or not such that the first bit of vector1 is ignored for comparison.

How can i modify my above code to do so?

Thanks

I'm not sure I understand what you want to do, but even more so I don't understand why you want to do it. Can you be more specific? I'm guessing you're trying to solve a non-issue.

If i have two vectors
vector1 = 1000
vector2 = 100

and i wish to compare the first three bits of vector 1 with first 3 bits of vector 2. can i do that in one statement without using for loop.

I think you're confusing people with the use of the word bit

So you want them to compare equal based on the length of the shorter vector? std::vector's operator== basically uses a test to see if the lengths match, then calls std::equal. You can manually call std::equal and skip the length check:

bool is_equal = false;

if ( vector1.size() < vector2.size() )
  is_equal = std::equal ( vector1.begin(), vector1.end(), vector2.begin() );
else
  is_equal = std::equal ( vector2.begin(), vector2.end(), vector1.begin() );

Hi,

I changed the code as follows and i think i should the value of out as 0. but i am getting the value of out as 1.

int main () {
  
  std::vector<int> vector1;
  std::vector<int> vector2;
  
  int out;
  
  bool is_equal = false;
 
  vector1.push_back(1000);
  vector1.push_back(1);
  vector1.push_back(0);
  vector1.push_back(0);
  vector2.push_back(1000);
  vector2.push_back(1);

  if ( vector1.size() < vector2.size() )
	is_equal = std::equal ( vector1.begin(), vector1.end(), vector2.begin() );
  else
	is_equal = std::equal ( vector2.begin(), vector2.end(), vector1.begin() );

  vector1 == vector2 ? out = 0 : out = 1;
  
  printf("out = %d \n", out);
  
  return 0;
}

Is there something wrong in my understanding.

Thanks for your help

>Is there something wrong in my understanding.
Probably. You're still doing the same thing as your first program. The value of out comes from vector::operator==, not is_equal. Compare:

#include <iostream>
#include <vector>

int main ()
{
  const int init1[] = {1000, 1, 0, 0};
  const int init2[] = {1000, 1};

  std::vector<int> vector1 ( init1, init1 + 4 );
  std::vector<int> vector2 ( init2, init2 + 2 );
  bool is_equal = false;

  if ( vector1.size() < vector2.size() )
    is_equal = std::equal ( vector1.begin(), vector1.end(), vector2.begin() );
  else
    is_equal = std::equal ( vector2.begin(), vector2.end(), vector1.begin() );

  std::cout<<"operator==: "<< ( vector1 == vector2 ) <<'\n';
  std::cout<<"std::equal: "<< is_equal <<'\n';
}

Thanks a lot ...

Help to modify, so Pn1, Pin2, and Pin3 are vector intead of arrays. And modify the testPIN function to accept a vetor instead of an array

//This program is a driver that tests a function comparing the
// contents of two int arrays.
#include <iostream>
using namespace std;

// Function Prototype
bool testPIN(int [], int [], int);

int main ()
{
   const int NUM_DIGITS = 7; // Number of digits in a PIN
   int pin1[NUM_DIGITS] = {2, 4, 1, 8, 7, 9, 0}; // Base set of values.
   int pin2[NUM_DIGITS] = {2, 4, 6, 8, 7, 9, 0}; // Only 1 element is
                                                 // different from pin1.
   int pin3[NUM_DIGITS] = {1, 2, 3, 4, 5, 6, 7}; // All elements are
                                                 // different from pin1.
   if (testPIN(pin1, pin2, NUM_DIGITS))
      cout << "ERROR: pin1 and pin2 report to be the same.\n";
   else
      cout << "SUCCESS: pin1 and pin2 are different.\n";

   if (testPIN(pin1, pin3, NUM_DIGITS))
      cout << "ERROR: pin1 and pin3 report to be the same.\n";
   else
      cout << "SUCCESS: pin1 and pin3 are different.\n";

   if (testPIN(pin1, pin1, NUM_DIGITS))
      cout << "SUCCESS: pin1 and pin1 report to be the same.\n";
   else
      cout << "ERROR: pin1 and pin1 report to be different.\n";
   system("");
   return 0;
}



This is the textPIN function


// The following function accepts two int arrays. The arrays are   *
// compared. If they contain the same values, true is returned.    *
// If the contain different values, false is returned.             *
//******************************************************************


#include <iostream>
using namespace std;

bool testPIN(int custPIN[], int databasePIN[], int size)
{
   for (int index = 0; index < size; index++)
   {
      if (custPIN[index] != databasePIN[index])
         return false; // We've found two different values.
   }
   return true; // If we make it this far, the values are the same.
}

i think you should Check std::mismatch method of C++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.