I want to use 2 pointers technique to find a pair of numbers that sum to a certain value using the c++ multiset container.
However why is it that the while loop doesn't stop when iterator l and iterator r in the my program reach the same element?
int main() {
multiset<int>st={3,2,1};
int value =6;
auto l=st.begin();
auto r=st.end()--;
while(l!=r){
if(*l+*r<value){
l++;
}
else if(*l+*r>value){
r--;
}
else{
cout<<*l<<" "<<*r<<endl;
break;
}
}
}
how to make the iterators stop when l and r are next to each other, with break added to the else condition, iterators stop when when both are pointing to the same element, so instead of 2 distinct numbers, both iterator are returning the same number.