Using an iterator without its container
Posted
by User1
on Stack Overflow
See other posts from Stack Overflow
or by User1
Published on 2010-06-02T03:35:30Z
Indexed on
2010/06/02
3:43 UTC
Read the original article
Hit count: 155
I am mixing some C and C++ libraries and have only a single pointer available to do some work in a callback function. All I need to do is iterate through a vector. Here's a simplified, untested example:
bool call_back(void* data){
done=...
if (!done) cout << *data++ << endl;
return done;
}
Note that this function is in an extern "C"
block in C++. call_back
will be called until true is returned. I want it to cout the next element each time it's called. data
is a pointer to something that I can pass from elsewhere in the code (an iterator in the above example, but can be anything). Something from data
will likely be used to calculate done
. I see two obvious options to give to data
:
- Have
data
point to my vector. - Have
data
point to an iterator of my vector.
I can't use an iterator without having the .end() method available, right? I can't use a vector alone (unless maybe I start removing its data). I could make a struct with both vector and iterator, but is there a better way? What would you do?
© Stack Overflow or respective owner