首页 > 解决方案 > 我无法计算出相等的范围

问题描述

所以我有这段代码,我在其中打印下限、上限和相等范围,但是如果有人知道我将如何提交编码解决方案,IDK 如何打印相等范围谢谢

#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;
}

标签: c++c++11vectoriteratorequal-range

解决方案


如果我理解正确,那么您的意思是以下

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

注意,而不是像这样的表达

c.f - ar.begin()

最好使用标std::distance头中声明的标准函数<iterator>。例如

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

请记住,引入隐藏众所周知的标准结构和标识符的宏是一种糟糕的编程风格。


推荐阅读