有一个代码:
for (int i = 0; i < n; i++){
cout << "i = " << i << endl;
cin >> q;
cout << "q = " << q << endl;
st.insert(q);
cout << "inserted" << endl;
curr_right += distance(st.lower_bound(q), st.begin());
cout << "end of for\n";
}
进入这样的测试时
5
5 4 2 3 1
程序显示的最后一件事
q = 3
inserted
然后它什么也不显示,也没有结束。
也就是说,插入set发生了,但函数的distance行为却很奇怪。
可能是什么问题呢?
distance 函数采用“正序”的两个迭代器,即 begin 必须是第一个。
在此更改之后,它似乎起作用了。如果没有,请提供更多数据,至少是完整的代码示例,而不是存根。