amp1590
amp1590

Reputation: 113

Is there any way to compare two consecutive elements of a std::set in c++?

I am using Codeblocks-13.12 and language C++. I wanted to compare two consecutive elements of a set using iterator. But the compiler gave error. I tried that just as a guess because I didn't get any info to do this by enough googling. If it is possible to do, then it would be too great.

The problem statement that I am working on is simple. I have given an array. I have to print a single integer,the maximum number of elements such that the absolute difference between any two of the integers is <= 1. The answer must be >=2.

I have solved this problem 100% correctly using array and set. Comparing two values of set, which I couldn't do using set, I have done that part using array. But if this part could be done using the std::set, then it would be too amazing and advantageous. My code using both set and array is -

#include <bits/stdc++.h>
#define for0(i,n)          for(int i=0;i<n;i++)
#define inf                100000000
using namespace std;
int a[103];
map<int,int> mp;
set<int> st;
set<int>::iterator it;

int main()
{
    int n,maxi=-inf,x;
    scanf("%d",&n);
    for0(i,n) {scanf("%d",&a[i]);mp[a[i]]++;st.insert(a[i]);}
    if(sz(st)==1){ pf("%d\n",mp[a[0]]);return 0;}
    for (it=st.begin(); it!=st.end(); ++it)
    {
        if(mp[*it]>1) 
        {
            x=mp[*it];
            if(x>maxi) maxi=x;
        }
    }
    sort(a,a+n);
    for(int i=0;i<n-1;i++)
    {
        if(a[i]!=a[i+1] && a[i+1]-a[i]==1)
        {
            int x=mp[a[i]]+mp[a[i+1]];
            if(x>maxi) maxi=x;
        }
    }
    if(maxi==-inf) printf("0\n");
    else  printf("%d\n",maxi);
    return 0;
}

This code is the 100% correct solution. But I wished to do the array part using set- that is comparing two consecutive element. My tried code is -

#include <bits/stdc++.h>
#define for0(i,n)          for(int i=0;i<n;i++)
#define inf                100000000
using namespace std;
int a[103];
map<int,int> mp;
set<int> st;
set<int>::iterator it;

int main()
{
    int n,maxi=-inf,x;
    scanf("%d",&n);
    for0(i,n) {scanf("%d",&a[i]);mp[a[i]]++;st.insert(a[i]);}
    if(sz(st)==1){ pf("%d\n",mp[a[0]]);return 0;}
    for (it=st.begin(); it!=st.end(); ++it)
    {
        if(mp[*it]>1) 
        {
            x=mp[*it];
            if(x>maxi) maxi=x;
        }
        if(it + 1 != st.end() && *(it + 1)-*it==1) 
        { 
            x=mp[*it]+mp[*(it + 1)]; 
            if(x>maxi) maxi=x; 
        } 
    }
    if(maxi==-inf) printf("0\n");
    else  printf("%d\n",maxi);
    return 0;
}

All the code is same as above except I added inside the "for" loop of set-

if(it + 1 != st.end() && *(it + 1)-*it==1) 
{ 
    x=mp[*it]+mp[*(it + 1)]; 
    if(x>maxi) maxi=x; 
}

instead of the array part -

sort(a,a+n);
for(int i=0;i<n-1;i++)
{
    if(a[i]!=a[i+1] && a[i+1]-a[i]==1)
    {
        int x=mp[a[i]]+mp[a[i+1]];
        if(x>maxi) maxi=x;
    }
}

In the line consisting of " if(it + 1 != st.end() && *(it + 1)-*it==1) " - here I am getting the error - "no match for operator+ in 'it + 1' ". So, this way is not correct. But I want to know that is there any way to do the described operation that I am trying to do using set? Please if anyone can help, it would be great.

Upvotes: 0

Views: 2559

Answers (1)

Saurav Sahu
Saurav Sahu

Reputation: 13974

Your problem in reduced form:

set<int> st;
set<int>::iterator it;
for (it=st.begin(); it!=st.end(); ++it)
{
    if(it + 1 != st.end() && *(it + 1)-*it==1) {  // << error
    //do something
    }
}

The compiler error no match for operator+ in 'it + 1' is self-explanatory. Instead you should use next:

if(next(it) != st.end() && *(next(it))-*it==1) 

Upvotes: 2

Related Questions