user6005857
user6005857

Reputation: 631

How to find the maximum in std::set< pair<int, int> > in a given range?

I have a set of pairs, and I want to find the maximum number in the second entry of the pair between l and r inclusive.

This is what the set looks like: myset = [(0,2),(1,1),(2,4),(3,0),(4,3)]

Here is what I have tried:

#include <iostream>
#include <set>
using namespace std;

#define INPUT1(x)  scanf("%d", &x)
#define INPUT2(x, y)  scanf("%d%d", &x, &y)
#define OUTPUT1(x) printf("%d\n", x);

bool cmp(pair<int, int> A, pair<int, int> B) {
    return A.second < B.second;
}

int main(int argc, char const *argv[]) {
    int n;
    INPUT1(n);

    set< pair<int,int> > myset;
    set< pair<int,int> >::iterator it;

    for (int i = 0; i < n; i++) {
        int val;
        INPUT(val);
        myset.insert(make_pair(i, val));
    }

    int l, r;
    INPUT2(l, r);
    int max = std::max_element(myset.begin()+l, myset.begin()+r+1, cmp)->second;
    OUTPUT1(max);
}

This doesn't work but for l = 1 and r = 3 I want is for max to equal 4.

I get the following error:

invalid operands to binary expression

('iterator' (aka '__tree_const_iterator<std::__1::pair<int, int>, std::__1::__tree_node<std::__1::pair<int, int>, void *> *, long>') and 'int')

Upvotes: 1

Views: 3672

Answers (4)

Saint-Martin
Saint-Martin

Reputation: 316

It still possible to accelerate this search for maximal, using a lambda which embeds an iterator :

int main (int argc, char* argv []) {
  std::pair<int, int> tmp [5] = {
    std::pair<int, int> (0,2),
    std::pair<int, int> (1,1),
    std::pair<int, int> (2,4),
    std::pair<int, int> (3,0),
    std::pair<int, int> (4,3)
  };

  std::set<std::pair<int, int> > s (tmp, tmp+5);
  size_t l (1), r (3);
  auto imax (s.begin ()), end (s.end ()), il (s.begin ()), ir (s.begin ());
  std::advance (il, l);
  std::advance (ir, r+1); 
  auto max ((int)0);
  auto i(il);
  std::for_each (il, ir, [&imax, &i, &max] (const auto& p) {
    if (p.second > max) {
      imax = i;
      max = p.second;
    }
    ++i;
  });
  std::cout << "*imax == (" << imax->first << ", " << imax->second << ")" << std::endl;
  return 0;
}

Upvotes: 1

Edgar Rokjān
Edgar Rokjān

Reputation: 17483

You cannot use std::max_element in such manner. The reason is that std::set provides bidirectional iterators, not random access iterators, so the things like myset.begin()+l are forbidden.

You should use something like this:

auto mx = std::numeric_limits<int>::min();

auto first = std::cbegin(myset);
std::advance(first, lf);

auto last = std::cbegin(myset);
std::advance(last, rg + 1);

for (auto it = first; it != std::cend(myset) && it != last; ++it) {
    mx = std::max(mx, it->second);
}

Upvotes: 2

Revolver_Ocelot
Revolver_Ocelot

Reputation: 8785

max_element returns an iterator to largest element. Not to mention that elements of set are pairs, not single integers.

Correct way to write it would be:

int max = std::max_element(myset.begin()+l, myset.begin()+r+1, cmp)->second;

Upvotes: 3

Boris Bolshem
Boris Bolshem

Reputation: 182

compare function should return TRUE if first is LESS than second repair

bool cmp(pair<int, int> A, pair<int, int> B) {
    return A.second < B.second;
}

Upvotes: 1

Related Questions