[英]Comparing a vector and a vector of struct for matching values - c++
我有一個包含 6 個數字的向量,另一個向量是我的結構的向量,它包含來自文件的數字數據。
我的向量是這樣的(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
我需要比較 vec1 和 vec2 以查看 vec2 中包含多少匹配數,並返回從 vec2 中的 vec1 中找到的每個值的計數。
我遇到的問題是我的 vec2 是一個結構向量,而 vec1 只是一個常規向量。 如何比較 vec 和結構的 vec? 我試過下面的方法,但是 '==' 運算符不喜歡 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
}
錯誤信息:操作數類型為:std::string == past_results
我如何明確地從我的結構向量中獲取值並與我的法線向量進行比較?
為了清楚起見,我刪掉了很多代碼,因為我認為它們不相關,因為我只需要知道如何比較這兩個向量。
在您的 if 語句中,您試圖比較 std::string 類型的 randomnums 元素與 past_result 類型的 resultvec 元素。 為了比較結構的實際 winningnums 值,將您的 if 語句更改為:
if(randomnums.at(i) == resultvec.at(i).winningnums)
{
//output result
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.