使用std::equalrange来搜索某些范围内的向量
using std::equal_range to search the vector within some ranges
我有一个std::向量,元素是对的,需要在一些范围内找到第一个分量的所有元素,例如,找到所有元素,使得abs(第一个分量值-引用)<0.01,我从的其他帖子中得到了代码
bool comp(pair<double, double> v1, pair<double, double> v2)
{
return (abs(v1.first-v2.first)<0.001);
}
vector< pair<double, double> data;
for (int i=0; i<10; i++)
{
double a, b;
// a & b are initialized randomly (the code was not shown here)
data.push_back(make_pair(a, b));
}
// we sort the vector here based on the pair.first (code not shown)
// compval is used as reference value, i.e. we are going to find all elements with abs(pair.first-ref.first)<0.01
pair<double, double> ref
ref.first = 0.5;
ref.second = 0.5;
std::pair< vector< pair<double, double>::iterator, vector< pair<double, double>::iterator> const range = std::equal_range(data.begin(), data.end(), ref, comp);
但是如果你观察由结果范围给出的所有元素,你会发现数据的所有元素而不是满足abs(pair.firstref.first)<将返回0.01。代码中有我遗漏的内容吗?谢谢
equal_range
需要一个排序的范围。
比较函数所隐含的顺序与将范围排序的顺序不匹配,因此对equal_range
的调用具有未定义的行为。
例如,假设您的列表包含{{.5, 0}, {.6, 0}}
(这些都经过排序),然后将std::bind(comp({.5,.5},_1))
应用于每个元素。
CCD_ 5将返回true。
comp({.5,.5},{.6,0})
将返回false。
您的排序顺序是:".5
小于.6
",但同时您的comp
函数是说".6
小于.5
"(因为有一个值小于.5
但不小于.6
)。这是矛盾的,也是你问题的原因。
要真正找到使std::bind(comp({.5,.5}, _1))
返回true的所有元素,可以使用std::copy_if(data.begin(), data.end(), some_output_iterator, std::bind(comp(ref, std::placeholders::_1)))
(尽管根据您的确切需求,有多种不同的方法)。
您的比较函数不满足严格弱排序的要求,即对于a和b这两个元素,如果comp(a, b) == true
,则comp(b, a)
应为false
。
你可能想要更像这样的东西:
struct Comp {
double target;
bool operator()(const pair<double, double>& lhs,
const pair<double, double>& rhs) const {
return abs(lhs.first - target) < abs(rhs.first - target);
}
};
vector< pair<double, double> > data;
data.push_back(make_pair(0.1, 1.0));
data.push_back(make_pair(0.15, 1.2));
data.push_back(make_pair(0.189, 2.1));
data.push_back(make_pair(0.19, -2.1));
data.push_back(make_pair(0.192, 3.1));
data.push_back(make_pair(0.2, 0.1));
data.push_back(make_pair(0.205, 0.1));
data.push_back(make_pair(0.205, 0.0));
data.push_back(make_pair(0.206, 12.1));
data.push_back(make_pair(0.21, -12.9));
data.push_back(make_pair(0.3, 3.4));
data.push_back(make_pair(0.5, 7.5));
// We *must* sort the vector using the same comparison function that we're
// about to use to get the lower and upper bounds
Comp comp;
comp.target = 0.2;
sort(data.begin(), data.end(), comp);
// Get the lower bound
pair<double, double> low(make_pair(comp.target, 0.0));
vector< pair<double, double> >::iterator lower =
lower_bound(data.begin(), data.end(), low, comp);
// Get the upper bound
pair<double, double> up(make_pair(comp.target + 0.01, 0.0));
// N.B. use "lower_bound" here to get upper bound of target + 0.01 exclusive.
// If we want to include target + 0.01, use "upper_bound"
vector< pair<double, double> >::iterator upper =
lower_bound(data.begin(), data.end(), up, comp);
for_each(lower, upper, [](const pair<double, double> &data_point) {
cout << data_point.first << "t" << data_point.second << "n";
});
输出:
0.2 0.1
0.205 0.1
0.205 0
0.206 12.1
0.192 3.1
0.21 -12.9
我认为应该在函数comp()
中使用fabs
而不是abs
。
- 为什么在全局范围内使用"extern int a"似乎不行?
- 错误:未在此范围内声明'reverse'
- 并行用于C++17中数组索引范围内的循环
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 不计算一个范围内的完美数
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 未在此范围内声明错误 'xy'
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 命名空间范围内的外部 - GCC vs clang vs msvc
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- 从给定范围内的向量中查找最大元素
- 如何在向量的给定索引范围内查找最小元素
- 在不复制的情况下构建范围内的向量
- 为什么变量在局部范围内声明,并放入范围外可见的向量中
- 使用std::equalrange来搜索某些范围内的向量
- 通过引用传递向量,并在基于for循环的范围内更改其值
- 当C++试图访问向量的元素(在范围内)时,Qt Quick Windows应用程序崩溃