c++ - 我无法计算出相等的范围
问题描述
所以我有这段代码,我在其中打印下限、上限和相等范围,但是如果有人知道我将如何提交编码解决方案,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;
}
解决方案
如果我理解正确,那么您的意思是以下
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 )
请记住,引入隐藏众所周知的标准结构和标识符的宏是一种糟糕的编程风格。
推荐阅读
- python - pycaret.anomaly.compare_models() 不工作;“没有属性 'compare_models'”,“'compare_models' 未定义”
- python - pip 将包安装到 python 3.9 文件夹中,但可执行命令在 python 3.8 中
- python - 在 Macbook pro M1 上的 tensorflow-metal 上运行 python 代码时出错
- flutter - Flutter web - 从pdf文件中获取文本
- go - 如何修复检查切片中最小数字以使用 Golang 进行步幅调度的函数?
- python - 从另一个 Dataframe 创建 Dataframe 并使用 DatetimeIndex 用 NaN 填充列
- python - NameError:名称'testing'未在vscode中定义
- c# - 将子控件项值发送到父表单文本框
- rust - 询问变量的附加值
- java - Teradata tdgssconfig.jar 和 terajdbc4.jar,传输失败 500 内部服务器错误