鑑於2套(C++)是有一種方便的方式來獲得交集的大小沒有任何alocations(如std :: set_intersection一樣)SetIntersection大小,而不分配
當然,我可以複製的執行減去分配但我一直不想重新發明輪子
int count = 0;
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) ++first1;
else if (*first2<*first1) ++first2;
else {
count++; ++first1; ++first2;
}
}
我使用std :: set_intersection並考慮通過一個「計數」迭代符......?
你的意思是它分配的標準:: set_intersection? – 4pie0 2014-09-10 11:46:28
@ 0d0a'std :: set_intersection'將公共元素複製到輸出迭代器中,並且通常需要爲容器分配內存,並且可能還會爲要複製的元素分配內存。 – hvd 2014-09-10 11:52:42
你的代碼看起來是正確的,這就是我要做的。你可能會構建一個「計數」迭代器,它不執行分配,但我懷疑它會比這更簡單。 – Beta 2014-09-10 11:55:57