Skip to main content
GameDev.net gamedev.net
🔒 Locked

iterators question

Started by CProgrammer Jan 29, 2004 at 4:30 AM 9 replies 400+ views
Original Post
CProgrammer
CProgrammer
How do I get the index value of the iterator returned by std::find? -CProgrammer
Sneftel
Sneftel
subtract the begin() iterator from your iterator. (this assumes you're using random access iterators.)


"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on January 29, 2004 5:34:38 AM]
siaspete
siaspete
It might only makes sense to do that for containers like vector, list, deque etc.

Try:

size_t index = (myIterator - myContainer.begin ());

I don't know if that will work on anything but vector, or does anyone know of a better (nicer) way?

Edit: Doh, Sneftel got there first :-)


Why you shouldn't use iostream.h - ever! | A Good free online C++ book


[edited by - siaspete on January 29, 2004 5:37:05 AM]
CProgrammer
CProgrammer
My code is pretty much STL free I just want std::find so I need something like:

int id = std::find...
Qw3r7yU10p!
Qw3r7yU10p!
std::find takes and return iterators

const int size = 50;
double array[size];
//... populate array with values


bool isLessThanSeven(double d) {
return d < 7;
}

//array is a double* and that is the iterator type returned

double* pos = std::find(array, array+size, isLessThanSeven);

int index = std::distance(array, pos);



[edited by - petewood on January 29, 2004 6:41:48 AM]
Fruny
Fruny

template<typename FwdIt, typename Elem>
size_t index(FwdIt first, FwdIt last, Elem value)
{
size_t index = 0;
for( ;first != last; ++first, ++index )
if(*first == value) return index;

return static_cast<size_t>(-1); // for the purists, that's how GNU

// libstdc++ defines std::string::npos

}

template<typename FwdIt, typename UnaryPred>
size_t index_if(FwdIt first, FwdIt last, UnaryPred pred)
{
size_t index = 0;
for( ;first != last; ++first, ++index )
if(pred(*first)) return index;

return static_cast<size_t>(-1); // for the purists, that's how GNU

// libstdc++ defines std::string::npos

}



“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on January 29, 2004 6:01:29 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
CProgrammer
CProgrammer
quote:
Original post by petewood
std::find takes and return iterators

const int size = 50;
double array[size];
//... populate array with values


bool isLessThanSeven(double d) {
return d < 7;
}

//array is a double* and that is the iterator type returned

double* pos = std::find(array, array+size, isLessThanSeven);

int index = std::distance(pos, array);



Im not too familiar with STL, so std::find basically returns a pointer to the array value found. Thanks.
Fruny
Fruny
quote:
Original post by CProgrammer
Im not too familiar with STL, so std::find basically returns a pointer to the array value found. Thanks.



Pointers are iterators to C arrays.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
CProgrammer
CProgrammer
petewood: Its std::distance(array, pos);
But it works great now thanks everybody.
Qw3r7yU10p!
Qw3r7yU10p!
quote:
Original post by CProgrammer
petewood: Its std::distance(array, pos);
But it works great now thanks everybody.


sorry CProgrammer, I went to edit it but got distracted by a phone call

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.