Heres some confusion about upper_bound() in cpp.

Revision en1, by rahimuj570, 2023-03-30 21:12:12

I don't understand how upper_bound work or give output

here's my code:

#include<bits/stdc++.h>
using namespace std;
int main(){

std::vector<int> v({2,4,1,3});
std::vector<int> v2({2,3,1,4});
auto it=upper_bound(v.begin(), v.end(),2);
auto it2=upper_bound(v2.begin(), v2.end(),2);
cout<<*it<<endl;
cout<<*it2;

return 0;}

I think upper_bound() give me next greater element of the vectors. So as according the value of *it and *it2 will be 4 and 3. But when I run the code, the output is *it=3 and *it2=4.

Output Screenshot link: https://prnt.sc/NqBCnAXtQR8P

I don't understand, what's going wrong. Can anybody clear this thing, please.

Tags upper_bound(), c++, confussion, upper_bound

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English rahimuj570 2023-03-30 21:12:12 813 Initial revision (published)