Given a std:: list
std::list< int > myList
and a reference (or a pointer) to an element in that list
int& myElement | int* pElement
So, basically I know the address of that element
How can I get an std::list<int>::iterator to that element efficiently?
A slow yet working example is
const_iterator it
for( it = myList.begin(); it != &myElement; ++it)
{
// do nothing, for loop terminates if "it" points to "myElem"
}
Is there a quicker way? like
const_iterator it = magicToIteratorConverter( myList, myElem )
Case of vector (but I need list):
For a vector, you could do the following:
const int* pStart = &myVector[0] // address of first element
const int* pElement = &myElem; // address of my element
const idx = static_cast< int >( pElement- pStart ); // no need to divide by size of an elem
std::vector< int >::iterator it = myVector.begin() + idx;