Date: Thu, 20 Feb 2025 15:35:26 -0800
DISCLAIMER: I assume I'm not the first one asking about this. so I did a
search in the list looking for it and spent a good 20 minutes going over
the results and didn't find anything,
Is there a reason std::stack does not provide a standard iterator, and for
that matter, an *input iterator* (or what would have been a legacy input
iterator)? Is it because it would be too confusing?
There could be uses for it, particularly using it in a range-for loop and
the algorithms (such as std::for_each, std::copy, std::copy_if, std::find,
std::find_if, std::all_of, std::any_of, std::none_of) that take as a
minimum an input iterator.
*An input iterator does not guarantee multiple passes*, so that should
avoid surprises—the stack is empty after iterating from beginning to end,
or the elements that have been traversed are gone.
The underlying sequence container is protected, so in order to gain access
to it one would have to derive and expose the iterators, or specialize
std::begin,
std::end but and allow friendship (other solutions are possible, I would
guess).
Here are a few of motivating examples:
// stack is empty after iteration
std::stack<int> pi ({6,1,4,1,3});
for (auto i : pi)
cout << i << endl;
// stack with one element left
std::find(pi.begin(), pi.end(), 3);
std::vector<int> dest{};
std::copy_if(pi.begin(), pi.end(), std::back_inserter(dest), [](int i) {
return i>3;
});
// or perhaps with function template overloads?
std::find(std::begin(pi), std::end(pi), 3);
search in the list looking for it and spent a good 20 minutes going over
the results and didn't find anything,
Is there a reason std::stack does not provide a standard iterator, and for
that matter, an *input iterator* (or what would have been a legacy input
iterator)? Is it because it would be too confusing?
There could be uses for it, particularly using it in a range-for loop and
the algorithms (such as std::for_each, std::copy, std::copy_if, std::find,
std::find_if, std::all_of, std::any_of, std::none_of) that take as a
minimum an input iterator.
*An input iterator does not guarantee multiple passes*, so that should
avoid surprises—the stack is empty after iterating from beginning to end,
or the elements that have been traversed are gone.
The underlying sequence container is protected, so in order to gain access
to it one would have to derive and expose the iterators, or specialize
std::begin,
std::end but and allow friendship (other solutions are possible, I would
guess).
Here are a few of motivating examples:
// stack is empty after iteration
std::stack<int> pi ({6,1,4,1,3});
for (auto i : pi)
cout << i << endl;
// stack with one element left
std::find(pi.begin(), pi.end(), 3);
std::vector<int> dest{};
std::copy_if(pi.begin(), pi.end(), std::back_inserter(dest), [](int i) {
return i>3;
});
// or perhaps with function template overloads?
std::find(std::begin(pi), std::end(pi), 3);
Received on 2025-02-20 23:35:38