gpt4 book ai didi

c++ - 多集lower_bound迭代器的位置

转载 作者:行者123 更新时间:2023-12-01 14:50:51 24 4
gpt4 key购买 nike

假设我有一个多重集A = {0,1,1,1,2}。如果执行以下操作:

multiset<int>::iterator it = A.lower_bound(2)

它返回一个迭代器。我可以轻松打印它的值。但是有什么办法知道它的位置吗?我的意思是它将返回4。

最佳答案

使用 std::distance

#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 );
}

关于c++ - 多集lower_bound迭代器的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34237612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com