假設我有一個多集A = {0,1,1,1,2}。如果執行以下操作:multiset lower_bound迭代器的位置
multiset<int>::iterator it = A.lower_bound(2)
它返回一個迭代器。我可以輕鬆打印它的價值。但是有什麼方法可以知道它的位置嗎?我的意思是它會返回4.
假設我有一個多集A = {0,1,1,1,2}。如果執行以下操作:multiset lower_bound迭代器的位置
multiset<int>::iterator it = A.lower_bound(2)
它返回一個迭代器。我可以輕鬆打印它的價值。但是有什麼方法可以知道它的位置嗎?我的意思是它會返回4.
#include <iterator>
#include <set>
int main()
{
multiset<int> A = { 0, 1, 1, 1, 2 };
multiset<int>::iterator it = A.lower_bound(2);
size_t dist = distance(A.begin(), it);
}
他們是否有任何快速的選擇? –
@伊扎茲阿布拉爾我不知道其他人 – Rabbid76
您可以使用['標準:: distance'(http://en.cppreference.com/w/cpp/iterator/distance)至從'A.begin()'得到一個偏移量。 –
我正在尋找一些快速的替代品。據我所知距離()的複雜度是O(n)。糾正我,如果我錯了 –
是的,它是O(n)該類型的迭代器([雙向迭代器](http://en.cppreference.com/w/cpp/concept/BidirectionalIterator)),並沒有真的沒有更快辦法。 –