admin管理员组文章数量:1123924
I have the following test program:
#include <vector>
#include <print>
#include <ranges>
int main() {
const std::vector<int> input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto output = input
| std::views::filter([](const int n) { std::print("{} ", n); return n % 3 == 0; })
| std::views::transform([](const int n) {return n * n; });
const std::vector<int> vector = std::ranges::to<std::vector>( output );
std::println("\ninput size: {} output size: {}", input.size(), vector.size() );
}
which, when compiled in Xcode 16.2 with c++23 mode, outputs this:
0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
input size: 11 output size: 4
I'm confused by this. Why is each item evaluated twice when being converted to std::vector?
When I try a simple for
cycle to print the elements, each item is only evaluated once:
#include <vector>
#include <print>
#include <ranges>
int main() {
const std::vector<int> input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto output = input
| std::views::filter([](const int n) { std::print("{} ", n); return n % 3 == 0; })
| std::views::transform([](const int n) {return n * n; });
int sum = 0;
for ( int i : output ) sum += i;
std::println("\nsum: {}", sum);
}
outputs:
0 1 2 3 4 5 6 7 8 9 10
sum: 126
The first program seems inefficient and makes me reconsider if I should start using ranges... is there a better way to write it?
I have the following test program:
#include <vector>
#include <print>
#include <ranges>
int main() {
const std::vector<int> input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto output = input
| std::views::filter([](const int n) { std::print("{} ", n); return n % 3 == 0; })
| std::views::transform([](const int n) {return n * n; });
const std::vector<int> vector = std::ranges::to<std::vector>( output );
std::println("\ninput size: {} output size: {}", input.size(), vector.size() );
}
which, when compiled in Xcode 16.2 with c++23 mode, outputs this:
0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
input size: 11 output size: 4
I'm confused by this. Why is each item evaluated twice when being converted to std::vector?
When I try a simple for
cycle to print the elements, each item is only evaluated once:
#include <vector>
#include <print>
#include <ranges>
int main() {
const std::vector<int> input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto output = input
| std::views::filter([](const int n) { std::print("{} ", n); return n % 3 == 0; })
| std::views::transform([](const int n) {return n * n; });
int sum = 0;
for ( int i : output ) sum += i;
std::println("\nsum: {}", sum);
}
outputs:
0 1 2 3 4 5 6 7 8 9 10
sum: 126
The first program seems inefficient and makes me reconsider if I should start using ranges... is there a better way to write it?
Share Improve this question edited yesterday Ted Lyngmo 117k7 gold badges80 silver badges130 bronze badges asked yesterday Tomas AndrleTomas Andrle 13.3k15 gold badges79 silver badges94 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 6std::ranges::to<std::vector>( output )
computes the length of output
before initializing the elements. That is, there are two iterations:
- The first iteration is used to determine the length of
output
(which is also the size of the resultingstd::vector
). - The second iteration is used to initialize the elements of the resulting
std::vector
.
This approach is usually more efficient than a single iteration, because it avoids reallocation during vector construction.
本文标签: cWhy is this stdview getting evaluated twiceStack Overflow
版权声明:本文标题:c++ - Why is this std::view getting evaluated twice? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736610295a1945427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Apple clang version 16.0.0 (clang-1600.0.26.6)
– Tomas Andrle Commented yesterdayto()
. – Ted Lyngmo Commented yesterday