Блог пользователя altrko

Автор altrko, история, 3 недели назад, По-английски

how to make find function on set/multiset correspond to last element of that kind? like on multiset with elements 1 7 7 7 8 9 i want iterator pointing on last 7 on index 3.

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
3 недели назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

Just use upper_bound and then decrement the iterator once.

  • »
    »
    3 недели назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks so does lower_bound and find function do thr same thing?

    • »
      »
      »
      3 недели назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      Nah, they both are different thing.

      But you can always check that, if your iterator is pointing towards the correct element you want to find.

  • »
    »
    3 недели назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    wow, how did I not know you could use upper_bound on sets

    • »
      »
      »
      3 недели назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      yeah you can use it on sets and multisets but you have to use it like this st.upper_bound(x)

»
3 недели назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
auto it = st.upper_bound(number);

if (it != st.begin())
    it--;

in this way the iterator pointing on last occurrence of the number and if the condition is false or the value in the iterator not equal the number then the number doesn't exist