altrko's blog

By altrko, history, 2 months ago, In English

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.

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
2 months ago, # |
  Vote: I like it +7 Vote: I do not like it

Just use upper_bound and then decrement the iterator once.

  • »
    »
    2 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      2 months ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      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.

      • »
        »
        »
        »
        2 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I dont get how are they different

        • »
          »
          »
          »
          »
          2 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Just search it on internet and you will get everything about it

        • »
          »
          »
          »
          »
          2 months ago, # ^ |
            Vote: I like it +6 Vote: I do not like it

          lower_bound returns an iterator to the smallest element that is at least $$$x$$$.
          upper_bound returns an iterator to the smallest element that is greater than $$$x$$$.

          • »
            »
            »
            »
            »
            »
            2 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            i meant difference between lower_bound and find function

            • »
              »
              »
              »
              »
              »
              »
              2 months ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              Imagine a set s of $$${1, 3, 5, 7}$$$. The followings will hold true:

              • s.find(4) == s.end()
              • s.lower_bound(4) == s.find(5)

              Hope that was clear.

              • »
                »
                »
                »
                »
                »
                »
                »
                2 months ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                so do u mean because 4 is not in the set it points to nothing?

  • »
    »
    2 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      2 months ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

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

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it
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