admin管理员组文章数量:1317357
I am trying to implement a function that computes n choose k. My idea is to generate the n
th row of Pascal's triangle, and return its k
th element. I would like to use a std::generator
for this.
The problem is I cannot find a way to get just the n
th yielded element.
My code looks something like this:
#include <generator>
#include <ranges>
#include <vector>
std::generator<std::vector<long>> pascal_triangle()
{
std::vector<long> current_row{1};
while (true)
{
co_yield current_row;
std::vector<long> next_row;
next_row.reserve(current_row.size() + 1);
next_row.push_back(1);
for (const auto& [left_element, right_element] : std::views::pairwise(current_row))
{
next_row.push_back(left_element + right_element);
}
next_row.push_back(1);
current_row = std::move(next_row);
}
}
long choose(const int n, const int k)
{
// This line does not compile.
const std::vector row = std::ranges::next(pascal_triangle(), n);
return row[k];
}
What do I need to do to get the n
th row?
I am trying to implement a function that computes n choose k. My idea is to generate the n
th row of Pascal's triangle, and return its k
th element. I would like to use a std::generator
for this.
The problem is I cannot find a way to get just the n
th yielded element.
My code looks something like this:
#include <generator>
#include <ranges>
#include <vector>
std::generator<std::vector<long>> pascal_triangle()
{
std::vector<long> current_row{1};
while (true)
{
co_yield current_row;
std::vector<long> next_row;
next_row.reserve(current_row.size() + 1);
next_row.push_back(1);
for (const auto& [left_element, right_element] : std::views::pairwise(current_row))
{
next_row.push_back(left_element + right_element);
}
next_row.push_back(1);
current_row = std::move(next_row);
}
}
long choose(const int n, const int k)
{
// This line does not compile.
const std::vector row = std::ranges::next(pascal_triangle(), n);
return row[k];
}
What do I need to do to get the n
th row?
1 Answer
Reset to default 7There are two problems with this line:
const std::vector row = ranges::next(pascal_triangle(), n);
The first is that std::ranges::next
takes an iterator, not a range. The second is that it returns an iterator, not an element. std::ranges::next(i, n)
is a generalization of i + n
, it's not a generalization of i[n]
.
Fixing both issues, this compiles:
const std::vector row = *std::ranges::next(pascal_triangle().begin(), n);
Of course, you could write your own function that does what you had wanted ranges::next
to do:
template <ranges::input_range R>
auto nth(R&& r, ptrdiff_t n) -> decltype(auto) {
return *ranges::next(ranges::begin(r), n);
}
本文标签: cGet n39th element yielded from a stdgeneratorStack Overflow
版权声明:本文标题:c++ - Get n'th element yielded from a std::generator - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741997893a2410416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
std::vector<long> pascal_triangle_nth(size_t n) { ... while(n-- > 0) { .... } return current_row; }
? Coroutines here add big performance overhead. – PiotrNycz Commented Jan 30 at 7:52current_row
). – PlsHelp Commented Jan 30 at 8:47