简体   繁体   English

比较向量和结构向量以匹配值 - c++

[英]Comparing a vector and a vector of struct for matching values - c++

I have a vector of 6 numbers, and another vector which is a vector of my struct that contains numeric data from a file.我有一个包含 6 个数字的向量,另一个向量是我的结构的向量,它包含来自文件的数字数据。

my vectors are as so (vec1 = 6 random nums, vec2 = file numeric data)我的向量是这样的(vec1 = 6 random nums, vec2 = file numeric data)

vec1 vec1

1, 2, 3, 4, 5, 6 1, 2, 3, 4, 5, 6

vec2 vec2

4, 5, 2, 7, 34, 76 4, 5, 2, 7, 34, 76

5, 7, 3, 7, 1, 23 5, 7, 3, 7, 1, 23

98, 76, 5, 9, 12, 64 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.我需要比较 vec1 和 vec2 以查看 vec2 中包含多少匹配数,并返回从 vec2 中的 vec1 中找到的每个值的计数。

The issue im having is that my vec2 is a vector of a structure, and vec1 is just a regular vector.我遇到的问题是我的 vec2 是一个结构向量,而 vec1 只是一个常规向量。 How do you compare a vec with a vec of a structure?如何比较 vec 和结构的 vec? I've tried the below but the '==' operator doesnt like the comparison between a vec and a struct.我试过下面的方法,但是 '==' 运算符不喜欢 vec 和结构之间的比较。

   //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错误信息:操作数类型为: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.为了清楚起见,我删掉了很多代码,因为我认为它们不相关,因为我只需要知道如何比较这两个向量。

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.在您的 if 语句中,您试图比较 std::string 类型的 randomnums 元素与 past_result 类型的 resultvec 元素。 In order to compare the actual winningnums value of the struct, change your if-statement to:为了比较结构的实际 winningnums 值,将您的 if 语句更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM