使用结构进行反射,对称或及时的测试集

  • 本文关键字:测试 对称 结构 反射 c++
  • 更新时间 :
  • 英文 :


我在我的代码

中具有此结构
struct Pair
{
  int x,y;
  friend bool operator==(Pair a, Pair b)
  {
     return a.x == b.x && a.y == b.y;
  }
  friend istream& operator>>(istream& is, Pair& a)
  {
    is >> a.x >> a.y;
    return is;
 }
 friend ostream& operator<<(ostream& os, Pair a)
 {
    os << '(' << a.x << ',' << a.y << ')';
    return os;
 }
};
int main(){
Pair p;
fstream file;
string fileName = "Text.txt";
file.open(fileName.c_str(), fstream::in);
if(!file){
    cout << "Could Not Open " << fileName << " File" << endl;
}else{
    int size = 43;
    int * myArray = new int[size];
    for(int i = 0; i < size; i++){
        file >> myArray[i];
    }
    printArray(myArray,size);
}
return 0;

}

从文本文件中读取

5 1 1 2 2 3 3 4 4 5 5

7 1 1 2 2 3 3 4 4 4 4 7 7 4 7 7 7

8 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64

此文件中有3个关系,每个文件都以一个int开头,int是该关系中的对数,然后随后对许多对。然后(如果不是eof)读取另一个int,然后又读了许多成对,依此类推。

如何将这些数据读取到我的结构对?

或多或少类似的东西:

#include <fstream>
#include <iostream>
#include <list>
#include <vector>
struct Pair
{
    int x,y;
    friend bool operator==(Pair a, Pair b)
    {
        return a.x == b.x && a.y == b.y;
    }
    friend std::istream& operator>>(std::istream& is, Pair& a)
    {
        is >> a.x >> a.y;
        return is;
    }
    friend std::ostream& operator<<(std::ostream& os, Pair a)
    {
        os << '(' << a.x << ',' << a.y << ')';
        return os;
    }
};
int main()
{
    std::fstream file;
    file.open("Text.txt", std::fstream::in);
    std::list<std::vector<Pair> > relations;
    int size = 0;
    while(file >> size)
    {
        relations.emplace_back(size);
        for(int i=0; i < relations.back().size() && file >> relations.back()[i]; i++);
    }
    //Print
    for(auto &r : relations)
    {
        std::cout << "[";
        for(auto &p : r)
        {
            std::cout << p;
        }
        std::cout << "]n";
    }
    return 0;
}

示例

最新更新