14,739 questions
11
votes
2
answers
339
views
Is comparing two spans pointing to the same container well defined?
Is the comparison of begin() and end() iterators of two std::spans that point to the same memory well defined ?
#include <span>
#include <cassert>
int main() {
int arr[5]{1,2,3,4,5};
...
0
votes
2
answers
64
views
Is there an idiomatic way to partition a Rust iterator into multiple iterators by Enum variant?
I am struggling to find a clean/idiomatic approach to partition an iterator into a fixed number of sub parts based on Enum variants.
Example
enum State {
A(A),
B(B),
C(C),
}
let states = vec!...
2
votes
3
answers
114
views
How to use output iterators without a container: Set intersection without storage
The C++ reference has this example for printing the intersection of two sets (or sorted containers):
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector&...
3
votes
1
answer
83
views
Java iterator of regex matches from large source
The basic problem is that I need to output all matches of a regex within a file, but there's a few properties that make a solution tricky:
The file could be very large, so the whole file cannot be ...
6
votes
1
answer
175
views
What operations are permitted on an invalidated iterator?
There is a footnote in the [iterator.requirements] section of the C++ standard which states the following:
The effect of dereferencing an iterator that has been invalidated is undefined.
This ...
-2
votes
1
answer
72
views
Java Iterator Views
I frequently would like to iterate over a view of some underlying data structure, e.g. a backwards iteration or only a slice of a list. The Java standard library has some support for multiple iterator ...
6
votes
2
answers
67
views
Why is &Range<T> not an iterator, but &mut Range<T> is?
Demo code using version rustc 1.86.0 (05f9846f8 2025-03-31)
fn main() {
let mut source = 1..3;
test(&mut source); // This is good. which means &mut Range<T> is Iterator.
...
1
vote
1
answer
131
views
Can `std::unordered_map::iterator` be implemented without reference to underlying map?
A while back I ported some of the C++ stdlib containers to environment where stdlib was not available. While iterators for contiguous and node-based containers were easy, I was stumped how to ...
3
votes
1
answer
131
views
Inconsistent output with std::any_of
I have two version of the same function using std::any_of and std::find_if for checking whether an int exist in a vector (returned from g.getEdges()).
However when I repeatedly run the std::any_of the ...
2
votes
1
answer
90
views
Why is STL common_iterator's `operator*()` method not always const qualified?
In libstdc++ there is the following code:
class common_iterator
{
...
[[nodiscard]]
constexpr decltype(auto)
operator*()
{
__glibcxx_assert(_M_index == 0);
return *...
3
votes
1
answer
91
views
Is it safe to access elements outside subrange but inside parent range?
Let's say I have an std::vector<int> arr of 5 elements { 1, 2, 3, 4, 5 }.
Is it safe to get a subrange from it like sub = std::span{ arr.begin() + 2, arr.end() } and dereference element at -1 ...
1
vote
1
answer
59
views
How do I move as many elements as possible from a Vec into a pre-existing slice?
I have src: Vec<Foo> and dst: &mut [Foo]. I want to move as many elements as possible from src into dst. Specifically:
If src is shorter than dst, then the beginning of dst should be ...
3
votes
2
answers
118
views
C++ Custom iterator dereferencing as a value instead of a reference
I am trying to create some C++ types that behave as containers but generate values on the fly (for efficiency reasons).
Here is what I would ideally want (example with a container that generates zeros)...
10
votes
2
answers
470
views
Is dereferencing std::span::end always undefined?
While browsing cppreference to see if there is any container (or adapter or view) whose end can be dereferenced, I stumbled upon std::span::end. The article has the usual note saying:
Returns an ...
2
votes
2
answers
132
views
Dereferencing an iterator like list.end() [duplicate]
Consider the following code:
#include <iostream>
#include <list>
int main() {
// Write C++ code here
std::list<int> li {0,1,2,3,8};
std::list<int>::iterator* p = ...