How to subtract pointers pointing to elements of a set?

Правка en3, от shanto_bangladesh, 2022-04-06 17:26:18

I tried to find the index of the upper_bound of an integer in a set.

set<int> s = {1,2,3,4,5};
int ind = s.upper_bound(2) - s.begin();
cout << ind << "\n";

But it's showing error. I did similar thing with vectors previously. Code looks like the following:

vector<int> v = {1,2,3,4,5};
int ind = upper_bound(v.begin(), v.end(), 2) - v.begin();
cout << ind << "\n;

The above code nicely executes the desired task. Why it's not working for set and multiset and what to do if we want to do the same task with a set or multiset without traversing the whole set?

Thanks for your patience!

Теги pointers, syntax, compiler errors

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en3 Английский shanto_bangladesh 2022-04-06 17:26:18 10
en2 Английский shanto_bangladesh 2022-04-06 17:25:13 8
en1 Английский shanto_bangladesh 2022-04-06 17:24:06 687 Initial revision (published)