gpt4 book ai didi

c++ - 将范围拆分为重叠范围的范围

转载 作者:可可西里 更新时间:2023-11-01 18:37:32 25 4
gpt4 key购买 nike

我尝试使用 Ranges-V3 库将值容器分割成一系列范围,以便相邻范围共享边界元素。

考虑以下几点:

using namespace ranges;

std::vector<int> v = { 1, 2, 3, 0, 4, 0, 5, 0, 6, 7, 8, 0, 0, 9 };
auto myRanges = v | /* something like adjacent split */
for_each( myRanges, []( auto&& range ){ std::cout << range << std::endl;} );

我想根据区域是否满足两个条件将范围划分为重叠的子范围:

  1. 元素的值是否为零
  2. 或与一个或多个值为零的元素相邻

期望的输出:

[1,2,3]
[3,0,4,0,5,0,6]
[6,7,8]
[8,0,0,9]

我的尝试:

auto degenerate =
[]( auto&& arg ){
return distance( arg ) < 2;
};

auto myRanges = v | view::split(0) | view::remove_if( degenerate );
for_each( myRanges, []( auto&& range ){ std::cout << range << std::endl;} );

输出:

[1,2,3]
[6,7,8]

我不知道该怎么做

  1. “插入”范围从 3 到 6
  2. “追加”8 到 9 的范围

最佳答案

如果我正确理解了您的要求,您可以根据 adjacent_find 实现一个生成器:

namespace detail {
template<typename IterT, typename SentT>
struct seg_gen_fn {
IterT it_;
SentT end_;
bool parity_ = true;

ranges::iterator_range<IterT> operator ()() {
if (it_ == end_) {
return {it_, it_};
}

auto n = ranges::adjacent_find(
it_, end_,
[p = std::exchange(parity_, !parity_)](auto const a, auto const b) {
return a && !b == p;
}
);
return {
std::exchange(it_, n),
n != end_ ? ranges::next(std::move(n)) : std::move(n)
};
}
};

template<typename RngT>
constexpr auto seg_gen(RngT&& rng)
-> seg_gen_fn<ranges::iterator_t<RngT>, ranges::sentinel_t<RngT>>
{ return {ranges::begin(rng), ranges::end(rng)}; }
} // namespace detail

auto const segmented_view = [](auto&& rng) {
return ranges::view::generate(detail::seg_gen(decltype(rng)(rng)))
| ranges::view::take_while([](auto const& seg) { return !seg.empty(); });
};

int main() {
auto const ns = {1, 2, 3, 0, 4, 0, 5, 0, 6, 7, 8, 0, 0, 9};
ranges::copy(segmented_view(ns), ranges::ostream_iterator<>{std::cout, "\n"});
}

Online Demo
并不像人们希望的那样简洁...:-[


这对于一次性代码来说可能没问题,但需要多做一些工作,它的可重用性会更高:

namespace detail {
namespace tag = ranges::tag;

template<
typename RngT, typename PredT, typename IterT = ranges::iterator_t<RngT>,
typename StateT = ranges::tagged_compressed_tuple<
tag::begin(IterT), tag::end(ranges::sentinel_t<RngT>),
tag::current(bool), tag::fun(ranges::semiregular_t<PredT>)
>
>
struct seg_gen_fn : private StateT {
constexpr seg_gen_fn(RngT&& rng, PredT pred)
: StateT{ranges::begin(rng), ranges::end(rng), true, std::move(pred)}
{ }

ranges::iterator_range<IterT> operator ()() {
StateT& state = *this;
auto& it = state.begin();
if (it == state.end()) {
return {it, it};
}

auto& parity = state.current();
auto n = ranges::adjacent_find(
it, state.end(),
[p = std::exchange(parity, !parity), &pred = state.fun()]
(auto const& a, auto const& b) {
return !pred(a) && pred(b) == p;
}
);
return {
std::exchange(it, n),
n != state.end() ? ranges::next(std::move(n)) : std::move(n)
};
}
};

template<typename RngT, typename PredT>
constexpr seg_gen_fn<RngT, PredT> seg_gen(RngT&& rng, PredT pred) {
return {std::forward<RngT>(rng), std::move(pred)};
}
} // namespace detail

auto const segmented_view = [](auto&& rng, auto pred) {
return ranges::view::generate(detail::seg_gen(decltype(rng)(rng), std::move(pred)))
| ranges::view::take_while([](auto const& seg) { return !seg.empty(); });
};

int main() {
auto const ns = {1, 2, 3, 0, 4, 0, 5, 0, 6, 7, 8, 0, 0, 9};
ranges::copy(
segmented_view(ns, [](auto const n) { return n == 0; }),
ranges::ostream_iterator<>{std::cout, "\n"}
);
}

Online Demo

概念检查和预测留作练习。

关于c++ - 将范围拆分为重叠范围的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42522853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com