簡體   English   中英

將向量與結構向量進行比較 - c++

[英]comparing vector to vector of struct - c++

我有一個程序可以創建一個包含 6 個隨機數的向量,以及另一個包含文件所有數字內容的向量。 我需要將 6 個數字與整個文件向量進行比較以找到所有匹配項。

我已經設法讓我的程序工作,但它似乎只匹配我文件中每行的第一個數字,並且不會循環遍歷該行的 rest。

//struct to store data from file
struct past_results {
   std::string date;
   std::string winningnums;
};

 //vector containing the 6 random numbers  
 int size = 6;   
 std::vector<int> numbers(size);

 //data from struct goes into this vector
 std::vector<past_results> resultvec;
 
 //function to search vector and match with vector of 6 nums
 for (const struct &e : resultvec){

 //compares 'resultvec.winningnums' data to 'numbers' vector
 for(int i = 0; i < size; i++)
        if(std::stoi(e.winningnums) == numbers.at(i)){
            std::cout << "number found:" << numbers.at(i) << std::endl;
            ++numcount;
        }  
  }

示例 output:

Numbers generated: 17 36 14 17 22 2 

Numbers from vec: 5,6,16,17,19,22,40
Numbers from vec: 6,16,23,34,35,40,37
Numbers from vec: 10,14,15,23,24,27,39
Numbers from vec: 12,23,28,2,36,40,18  //<--- should catch the 2 from this line
Numbers from vec: 10,2,24,31,36,47,41
Numbers from vec: 13,17,22,41,43,47,28
Numbers from vec: 2,11,28,29,31,39,24
number found:2
Numbers from vec: 4,12,15,39,45,47,22
Numbers from vec: 1,2,33,36,38,45,48   //<---- should catch 2 from this line
Numbers from vec: 2,15,16,17,25,26,44
number found:2

你可以看到我的程序每行只檢查一次值,並沒有運行整行來檢查。 如果它是行中的第一個數字,它也只會獲得匹配的數字。 我需要程序逐行搜索,並從每個字符串中找到所有匹配的數字。 文件內容為字符串形式,由我的文件中的 getline function 收集,然后我使用 stoi:: 將它們轉換回 int

我將如何擴展我的 for 循環以包含該行的所有數據而不僅僅是第一個數字?


編輯

最小的例子:

//struct to store data from file
struct foo {
   std::string b;
};


int main(){

   std::vector<foo> a;
   
   //vector of nums to compare
   std::vector<int> c;
    
   //find numbers in vector b that equal vector of struct foo(element x)
   if(c.at(i) == a.b.at(i))
   {
     //output matching nums
    
   }

 }

代替

struct foo {
   std::string b;
};

我會將您從文件中讀取的數字存儲在存儲數字的容器中,例如std::vector<int>如果您需要按照它們在文件中出現的確切順序存儲它們。 如果您不需要保留順序,您也可以std::sort它們通過使用二進制搜索算法來加快搜索速度。 如果您不需要支持存儲重復項,則可以將數字直接存儲在std::unordered_set中。

我不知道您需要什么,所以我將展示如何按照文件中列出的方式存儲它們,然后在檢查您搜索的數字是否存在時使用std::unordered_set

#include <iostream>
#include <sstream>
#include <unordered_set>
#include <vector>

//------------------------------------------------------------------------------
// a helper to discard a certain character from an istream
template<char Char> struct eat {};

template<char Char>
std::istream& operator>>(std::istream& is, const eat<Char>&) {
    if(is.peek() == Char) is.ignore();
    else is.setstate(std::ios::failbit);
    return is;
}
//------------------------------------------------------------------------------
// struct to store data from file
struct foo {
    std::vector<int> b;
    // ...other members...

    // return the stored numbers as an unordered_set to make searching easy
    std::unordered_set<int> get_as_set() const {
        return {b.begin(), b.end()};
    }
};

// read one `foo` from an istream:
std::istream& operator>>(std::istream& is, foo& f) {
    std::string line;
    if(std::getline(is, line)) {
        std::istringstream iss(line);
        f.b.clear();

        int number;
        while(iss >> number) {
            f.b.push_back(number);
            iss >> eat<','>{};
        }
    }
    return is;
}

// print one `foo` to an ostream:
std::ostream& operator<<(std::ostream& os, const foo& f) {
    auto it = f.b.begin();
    if(it != f.b.end()) {
        os << *it;
        for(++it; it != f.b.end(); ++it) {
            os << ',' << *it;
        }
    }
    return os;
}
//------------------------------------------------------------------------------
int main() {
    // an example file:
    std::istringstream file{
        R"aw(5,6,16,17,19,22,40
6,16,23,34,35,40,37
10,14,15,23,24,27,39
12,23,28,2,36,40,18
10,2,24,31,36,47,41
13,17,22,41,43,47,28
2,11,28,29,31,39,24
4,12,15,39,45,47,22
1,2,33,36,38,45,48
2,15,16,17,25,26,44
)aw"};

    // read the file into a vector<foo>:
    std::vector<foo> a;
    foo tmp;
    while(file >> tmp) {
        a.push_back(std::move(tmp));
    }

    // vector of nums to compare
    std::vector<int> c{7, 36, 14, 17, 22, 2};

    // find numbers in vector b that equal vector of struct foo(element x)
    for(const foo& f : a) {
        std::cout << f << '\n';  // print the `foo`
        auto s = f.get_as_set(); // get the numbers in `f` as a set

        for(int val : c) {
            // check if the set contains the values you search for:
            if(s.contains(val)) std::cout << val << " found\n";
            // s.contains(val) requires c++20 or greater, for earlier
            // versions, use `s.find(val) != s.end()`
        }
    }
}

Output:

5,6,16,17,19,22,40
17 found
22 found
6,16,23,34,35,40,37
10,14,15,23,24,27,39
14 found
12,23,28,2,36,40,18
36 found
2 found
10,2,24,31,36,47,41
36 found
2 found
13,17,22,41,43,47,28
17 found
22 found
2,11,28,29,31,39,24
2 found
4,12,15,39,45,47,22
22 found
1,2,33,36,38,45,48
36 found
2 found
2,15,16,17,25,26,44
17 found
2 found

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM