Home > front end >  Comparing a vector and a vector of struct for matching values - c
Comparing a vector and a vector of struct for matching values - c

Time:04-15

I have a vector of 6 numbers, and another vector which is a vector of my struct that contains numeric data from a file.

my vectors are as so (vec1 = 6 random nums, vec2 = file numeric data)

vec1

1, 2, 3, 4, 5, 6

vec2

4, 5, 2, 7, 34, 76

5, 7, 3, 7, 1, 23

98, 76, 5, 9, 12, 64

I need to compare vec1 to vec2 to see how many matching numbers are contained in vec2, and return the count of each value found from vec1 in vec2.

The issue im having is that my vec2 is a vector of a structure, and vec1 is just a regular vector. How do you compare a vec with a vec of a structure? I've tried the below but the '==' operator doesnt like the comparison between a vec and a struct.

   //struct to store data from file
   struct past_results {
      std::string date;
      std::string winningnums;
    };
    
    //vectors to store file content, and 6 random numbers
    std::vector<past_results> resultvec;
    std::vector<std::string> random_nums

    for(int i = 0; i < resultvec.size(); i  )
    if(randomnums.at(i) == resultvec.at(i))
    {
     //output result
    }

error message: operand types are: std::string == past_results

How do I explicitly get the values from my struct vector and compare to my normal vector?

For clarity, I have cut out a lot of my code as I didn't believe it to be relevant as i just need to know how I would compare both vectors.

CodePudding user response:

In your if statement you are trying to compare an element of randomnums which is of type std::string to an element of resultvec which is of type past_result. In order to compare the actual winningnums value of the struct, change your if-statement to:

if(randomnums.at(i) == resultvec.at(i).winningnums)
{
    //output result
}
  • Related