Background: Ubuntu OS is installed on my machine, gcc version is 5.4.0. I can freely call the erase method (for a variable of type std::vector , for example) passing it the const_iterator parameter to the item to be deleted. On the other machine, the same Ubuntu OS is installed, but gcc version 4.8.4 is installed - and, as I understand it, in this version (although it supports compilation with the std=c++11 flag), the erase method erase accepts an iterator .
In general, the code looks like this:
void SomeoneClass::method( ... ) { Subscribers::const_iterator pos = anotherMethod( ... ); if( pos != m_subscribers.end() ) { m_subscribers.erase( pos ); } } I decided to add a check for the version of gcc being used, sort of
#if defined( __GNUC__ ) && ( __GNUC__ < 5 ) // тут получаем номер элемента через pos и т.д #else Subscribers::const_iterator pos = anotherMethod( ... ); if( pos != m_subscribers.end() ) { m_subscribers.erase( pos ); } #endif There were questions:
- Am I right to do this comparison:
const_iterator pos != container.end()? - How to get an element number through const_iterator, and then get an iterator using this number? (remember, gcc 4.8) Something like this:
Subscribers::difference_type itemPos = pos - m_subscribers.begin(); Subscribers::iterator it = m_subscribers.begin() + itemPos; UPD: I beg your pardon. We consider not std::vector , but std::list
consttransformation is the same. - aleks.andr