#include <iostream> #include <cstdio> #include <vector> #include <iterator> #include <algorithm> #include <string> #include <set> #include <map> using namespace std; //cout<< //cin>> set<long long int>::iterator it; set<long long int>::iterator it1; bool pd(set<long long int > set1,set<long long int> set2) { if(set1.size()!=set2.size())return false; for(it=set1.begin(),it1=set2.begin();it!=set1.end();it++,it1++) { if(*it!=*it1)return false; } return true; } int main() { set<long long int> set1,set2; long long int x; while((cin>>x)) { if(x!=0)set1.insert(x); while(cin>>x&&x!=0) {set1.insert(x);} while(cin>>x&&x!=0) { set2.insert(x); } if(pd(set1,set2)==true)cout<<"YES"<<endl; else cout<<"NO"<<endl; set1.clear(); set2.clear(); } // // set_intersection()//交集 // set_union()//并集 // set_difference()//差集 // set_symmetric_difference() return 0; }这段代码哪里有问题
时间: 2023-11-28 12:47:31 浏览: 114
这段代码没有明显的语法错误,但是存在以下问题:
1. 函数 `pd()` 中的两个 `set` 传入的参数应该是 `const set<long long int>&` 类型,因为函数内部并不修改这两个参数。
2. 函数 `pd()` 中的两个 `set` 比较可以使用 `std::set::operator==` 来直接比较两个 `set` 是否相等,无需手动遍历比较。
3. 在输入数据时,当输入一个数为0时,程序会跳出第一个循环,但是没有清空输入流中的其他字符,导致第二个循环开始时读取到的是上一次输入流中的剩余字符,进而导致结果错误。
以下是修改后的代码:
```
#include <iostream>
#include <set>
using namespace std;
bool pd(const set<long long int>& set1, const set<long long int>& set2) {
return set1 == set2;
}
int main() {
set<long long int> set1, set2;
long long int x;
while (cin >> x) {
if (x != 0) {
set1.insert(x);
}
else {
break;
}
}
while (cin >> x) {
if (x != 0) {
set2.insert(x);
}
else {
break;
}
}
if (pd(set1, set2)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
return 0;
}
```
阅读全文