求助指针

灌水区

ningago @ 2022-06-25 11:37:32

RT,问个低级问题,

对于set <int> s给定指针set<int>::iterator it=s.xxx,如何求出它和s.end()之间差了多少个元素?


by StarLbright40 @ 2022-06-25 11:39:29

暴力。

建议换成手写平衡树(

迭代器和指针好像还有区别,但我不懂这个/kk


by dehsirehC @ 2022-06-25 11:40:15

楼上正解


by fjy666 @ 2022-06-25 11:41:10

std::distance()?


by xyf007 @ 2022-06-25 11:41:22

@ningago std::distance,但是复杂度高。


by ningago @ 2022-06-25 11:41:42

@xyf007 O(1) 带大常?


by Celtic @ 2022-06-25 11:41:46

只能 O(n) 枚举吧


by sprads @ 2022-06-25 11:42:47

setdistance 就是暴力


by xyf007 @ 2022-06-25 11:43:58

@ningago O(n)


by ningago @ 2022-06-25 11:45:06

@xyf007 谢(那我为什么不手写while(it!=s.end())++it,++cnt;


by xyf007 @ 2022-06-25 11:46:17

@ningago 因为 std::distance(it, s.end()) 更好写。


| 下一页