Issue
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')
Solution
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);
}
Answered By - Edgar RokjÄn Answer Checked By - David Goodson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.