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

Автор anajin, 12 лет назад, По-английски

I search it in google , but i can't find it. Can somebody tell me how to use lower_bound in set<pair<int,int>> , so i can find the fist pair whose first element is not small than the element i search for ?

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

»
12 лет назад, скрыть # |
 
Проголосовать: нравится +35 Проголосовать: не нравится

x.lower_bound({first, -inf});

»
12 лет назад, скрыть # |
 
Проголосовать: нравится -18 Проголосовать: не нравится

This is a pretty good site : http://bit.ly/1DMMnub

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

How can i call lower_bound on pair<int,int> to get the last occurence of pair if element is repeating ?

For eg :

[7,1,5,3,1,1]

[ {1,1,} , {1,4} , {1,5}, {3,3}, {5,2}, {7,0} ]

if i do lower_bound for should get pair {1,5}

  • »
    »
    7 лет назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится +11 Проголосовать: не нравится

    try auto it = st. upper_bound({first+1,-inf})
    if(it == st.begin()) element not found
    else element in it--

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

      can u explain more ?

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

        upper bound return an iterator that points to the first element strictly bigger than {x,y}
        st. upper_bound({first+1,-inf}) return an iterator to the first element strictly bigger than the element we are looking for
        for example :

        [ {1,1,} , {1,4} , {1,5}, {3,3}, {5,2}, {7,0} ]
        auto it = st. upper_bound({2,-inf})
        *it = {3,3}
        so to get {1,5} all we need to do is it--;
        A corner case is :
        [ {3,3}, {5,2}, {7,0} ]
        auto it = st. upper_bound({2,-inf})
        *it = {3,3} (it = st.begin())
        in this case it-- is not defined so this will cause a runtime error
        tha's why we need to test if (it == st.begin())
        PS : in our case lower_bound will give the same result as we are sure that no pair have the second element equals -inf so stricly bigger ( < ) or stricly bigger or equal ( <= ) gives the same result

»
7 лет назад, скрыть # |
 
Проголосовать: нравится -13 Проголосовать: не нравится

you can do it this way : s.lower_bound(make_pair(x, y)) or s.lower_bound(pair(x, y)). (the last case just in c++17).