【发布时间】:2020-06-16 08:22:08
【问题描述】:
我必须在 n 个元素的列表中找到唯一元素的数量。 我已经使用了 set 并且它被接受了,但是当我在其中一种情况下使用 unordered_set 时超出了时间限制。这怎么可能?
使用集合的代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
set<int> s;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
s.insert(x);
}
cout << s.size() << "\n";
return 0;
}
使用 Unordered_set 的代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
unordered_set<int> s;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
s.insert(x);
}
cout << s.size() << "\n";
return 0;
}
【问题讨论】:
标签: c++ data-structures c++14