将一系列已排序的元素划分为相邻组

partitioning a range of sorted elements into adjacent groups

本文关键字:划分 元素 一系列 排序      更新时间:2023-10-16

我在下面有一个排序的项目列表。 我需要确定如何从此列表中选择 [包含、排除(项目对,以便它们之间的差异超过某个固定值(例如以下示例中的 5(。

因此,这应该导致将列表划分为相邻的范围(不遗漏任何元素(。

我想出了一种蛮力方法来做到这一点(参见现场 COLIRU 演示(,但我相信一定有一个更优雅的解决方案,而且我可能错过了一些边缘情况(例如 1 包含单个值的列表应该导致空对(。 我在想 stl 范围算法、std::adjacent_findstd::lower_bound/std::upper_bound的组合可以用来确定这些包含/排除对 - 例如在循环中使用或某种基于范围的搜索或某种排序 - 但我无法弄清楚。

实时搜索值{ 100, 104, 108, 112, 116, 120 },从而产生以下非重叠范围。 请注意,最后一对(差值为 4(即 <5(是一种特殊情况(请参阅代码(。

[100,104),[108,112),[116,120)

执行此操作的代码如下:

#include <iostream>
#include <algorithm>
#include <experimental/iterator>
#include <string>
#include <vector>
int main()
{
std::vector<int> elements = { 100, 104, 108, 112,  116, 120 };
std::vector<std::pair<int, int>> result;
auto current = elements.begin();
while (current != std::prev(elements.cend())) {
auto next = std::next(current);
while (((*next - *current) < 5) && (next != std::prev(elements.cend()))) {
++next;
}
// consider edge case where we are at the end of the list
if (next != std::prev(elements.cend())) {
result.emplace_back(*current, *std::prev(next));
} else {
result.emplace_back(*current, *next);
}
current = next;
}
std::transform( result.cbegin(), result.cend(), std::experimental::make_ostream_joiner(std::cout, ","), 
[](const auto& next){ return std::string("[") + std::to_string(next.first) + ',' +  std::to_string(next.second) + ')'; } );  
}
auto next = std::next(current);
while (((*next - *current) < 5) && (next != std::prev(elements.cend()))) {
++next;
}

排序列表中,我们正在寻找比当前元素至少大 5的第一个元素,对吗?这正是std::lower_bound的用途 - 它执行二进制搜索而不是线性搜索:

auto next = std::lower_bound(std::next(current), elements.end(), *current + 5);

将其与修复循环条件相结合,直到列表的末尾而不是结束之前(这只是......看起来不对劲,需要一些严肃的理由(,整个身体可以只是:

while (current != elements.end()) {
auto next = std::lower_bound(std::next(current), elements.end(), *current + 5);
result.emplace_back(*current, *std::prev(next));
current = next;
}
<小时 />

旁注。这:

std::transform( result.cbegin(), result.cend(), std::experimental::make_ostream_joiner(std::cout, ","),·
[](const auto& next){ return std::string("[") + std::to_string(next.first) + ',' +  std::to_string(next.second) + ')'; } );

对我来说,似乎没有比这个更好的了:

bool first = true;
for (auto const& [first, second] : result) {
if (!first) std::cout << ',';
first = false;
std::cout << '[' << first << '',' << second << ']';
}

扬子晚报.我知道人们喜欢说"没有原始循环",但我很少看到transform导致可读的代码......