如何测试反身,对称或传递 [英] How can I test for reflexive, symmetric, or transitive
问题描述
我必须在我的代码中使用这个结构:
struct Pair
{
int x ,y;
friend bool operator ==(Pair a,Pair b)
{
return a.x == b.x&& a.y == b.y;
}
friend istream&运算符>>(istream& is,Pair& a)
{
是> a.x>> a.y;
return is;
}
friend ostream& operator<<<(ostream& os,Pair a)
{
os< '('<< a.x<<','<< a.y<<')';
return os;
}
};
我需要读取.txt文件:
5 1 1 2 2 3 3 4 4 5 5
7 1 1 2 2 3 3 4 4 4 7 7 4 7 7
8 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64
此文件有3种关系,每个都以一个int开始,int是该关系中的对的数量,那么很多对跟随。
如何读取这些数据到我的struct对中?
我读完后,我将测试数据是否是反身等等,但是我只是在开始这个项目时遇到麻烦。
这是我写的。
我知道我的想法可能是一个高级但至少它应该告诉你,所需的代码真的不是那么多。
解释部分:
-
istringstream
)允许您将单个行视为流。 -
朋友
operator>>
)将允许你使用简单的方式从流中只读一对:Pair p;
stream>> p;
现在,这是下面的代码
对
对
对
istream_iterator< Pair>
using Pairs = vector< Pair> ;;
int main()
{
string line;
while(getline(cin,line))
{
istringstream iss(line);
unsigned n;
对;
if(iss>> n)
copy_n(istream_iterator< Pair>(iss),n,back_inserter(pairs)
if(!iss)
return 255;
std :: cout<< Read a line with<< n<< pairs(check:<< pairs.size()<<)\\\
;
}
}
查看 Live on Coliru 输入问题的样例,打印:
读取一行5对(检查:5)
读取一行7对(检查:7)
读取一行8对(支票:8)
I have to use this struct in my code:
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;
}
};
I need to read a .txt file:
5 1 1 2 2 3 3 4 4 5 5
7 1 1 2 2 3 3 4 4 4 7 7 4 7 7
8 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64
This file has 3 relations in it, each one starts with an int which is the number of pairs in that relation, then that many pairs follows. Then (if not eof) read another int and that many pairs again, and so on.
How can I read this data into my struct pair?
After I read it I will have to test if the data is reflexive, etc, but I am just having trouble getting started on this project.
Here's what I'd write.
I know my take on it is probably a bit 'advanced' - but at least it should show you that the code required is really not that much.
On bit of explanation:
using a string stream (
istringstream
) allows you to treat a single line as a stream. This isn't strictly necessary, but prevents things running awry if the input isn't in the expected format.The
friend
stream operators (in particular,operator>>
) will allow you to "just" read a pair from a stream with a simple:Pair p; stream >> p;
Now, this is what the code below implicitely does when I invoke the
copy_n
algorithm on anistream_iterator<Pair>
(i.e. it extractsPair
s in exactly the same fashion as I just showed).
using Pairs = vector<Pair>;
int main()
{
string line;
while (getline(cin, line))
{
istringstream iss(line);
unsigned n;
Pairs pairs;
if (iss >> n)
copy_n(istream_iterator<Pair>(iss), n, back_inserter(pairs));
if (!iss)
return 255;
std::cout << "Read a line with " << n << " pairs (check: " << pairs.size() << ")\n";
}
}
See it Live on Coliru with the sample input from the question, printing:
Read a line with 5 pairs (check: 5)
Read a line with 7 pairs (check: 7)
Read a line with 8 pairs (check: 8)
这篇关于如何测试反身,对称或传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!