任何人都可以解釋無序集合是如何工作的嗎?我也不確定一個集合是如何工作的。我的主要問題是它的查找功能的效率是多少。Unordered_set questions
例如,這個總的大O運行時間是多少?
vector<int> theFirst;
vector<int> theSecond;
vector<int> theMatch;
theFirst.push_back(-2147483648);
theFirst.push_back(2);
theFirst.push_back(44);
theSecond.push_back(2);
theSecond.push_back(-2147483648);
theSecond.push_back(33);
//1) Place the contents into a unordered set that is O(m).
//2) O(n) look up so thats O(m + n).
//3) Add them to third structure so that's O(t)
//4) All together it becomes O(m + n + t)
unordered_set<int> theUnorderedSet(theFirst.begin(), theFirst.end());
for(int i = 0; i < theSecond.size(); i++)
{
if(theUnorderedSet.find(theSecond[i]) != theUnorderedSet.end())
{
theMatch.push_back(theSecond[i]);
cout << theSecond[i];
}
}
效率!=大O. – 2011-06-01 17:24:07
有答案涵蓋了你的問題,或者是還不清楚的東西? – juanchopanza 2011-06-09 06:58:09