0

So I have this code in which I print lower, upper, and equal range but IDK how to print equal range if someone knows how I will like a coded solution to be submitted thanks

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<long long> vi;
typedef pair<long long,long long> pi;
typedef vector<pi> vpi;

#define FOR(i, a, b) for(ll i=ll(a); i<ll(b); i++)
#define ROF(i, a, b) for(ll i=ll(a); i>=ll(b); i--)
#define f first
#define s second
#define pb emplace_back
#define mp make_pair
#define SQ(a) (a)*(a)
#define all(a) (a).begin(), (a).end()

int main() {
    ll n,x=6,s;
    cin>>n;

vi ar;

FOR(i,0,n)cin>>s; ar.pb(s);

auto a = lower_bound(all(ar), x)-ar.begin();

auto b = upper_bound(all(ar), x)-ar.begin();

auto c = equal_range(all(ar), x);

cout<<"lower_bound "<<a<<' '<<"upper_bound"<<' '<<b<<' '<<"equal range.first"<<' '<<c.f<< ' '<<"equal range.second"<<' '<<c.s<<"\n";
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Do you know why you're subtracting `ar.begin()` for `a` and `b`? – eesiraed Apr 17 '20 at 21:45
  • Please, don't use macros for abbreviations. If you need abbreviations, please take a keyboarding class. The abbreviation macros make reading the code more difficult, doesn't speed up the build time and does not speed up the execution time. – Thomas Matthews Apr 17 '20 at 21:46
  • I do not but still when I do the same to auto c; or c.f,c.s individually it won't work – Nothingnoname Apr 17 '20 at 21:47
  • You may want to look up examples of obfuscated code at the [International Obfuscated Code Contest](https://www.ioccc.org/) for examples of unreadable code. – Thomas Matthews Apr 17 '20 at 21:48
  • Well then stop [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming) and you won't have this problem. This is an example of why you shouldn't use code that you don't understand. Learn about how iterators work. – eesiraed Apr 17 '20 at 21:50

1 Answers1

0

If I have understood correctly then what you mean is the following

cout << "lower_bound " << a <<' '
     << "upper_bound " <<' '<< b <<' '
     << "equal range.first " << ' ' << c.f - ar.begin() << ' '
     << "equal range.second" << ' ' << c.s - ar.begin() << "\n";

Pay attention to that instead of expressions like for example this

c.f - ar.begin()

it is better to use the standard function std::distance declared in the header <iterator>. For example

std::distance( ar.begin(), c.f )

Bear in mind that it is a bad style of programming to introduce macros that hides well-known standard constructions and identifiers.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335