Qt: Iteratore di un vettore in un QMap
Domanda
Sto lavorando con Qt e OpenCV e vorrei creare un iteratore con std::vector <cv::Rect_<int>>
per avere accesso a tutti i cv :: redd_.
Questo vettore fa parte di un QMap < int, std::vector <cv::Rect_<int>> > _facesframe;
Quindi è così che sto cercando di avere accesso a questi vettori:
foreach (unsigned int frame , _imageGItem->_faceSampler._facesframe.keys() )
{
std::vector <cv::Rect_<int>>::const_iterator it = _imageGItem->_faceSampler._facesframe.value(frame).begin();
if( it != _imageGItem->_faceSampler._facesframe.value(frame).end())
{
qDebug()<<"here";
}
}
.
Ma il programma si blocca sulla linea if...
a causa di un iteratore incompatibile.
Qualcuno sa come raggiungere tutto cv::Rect_<int>
di un QMap < int, std::vector <cv::Rect_<int>> >
per favore?
Soluzione
Questo perché stai confrontando i iteratori in diversi vettori.
const T QMap::value(const Key & key, const T & defaultValue = T()) const
.
Vector viene restituito per valore, quindi questo viene copiato.
Dovresti usare
T & QMap::operator[](const Key & key)
.
Per correggere questo:
foreach (unsigned int frame , _imageGItem->_faceSampler._facesframe.keys() )
{
std::vector <cv::Rect_<int>>::const_iterator it =
_imageGItem->_faceSampler._facesframe[frame].begin();
if( it != _imageGItem->_faceSampler._facesframe[frame].end())
{
qDebug()<<"here";
}
}
.
o (Bacause meno efficiente di 1 copia in esecuzione):
std::vector <cv::Rect_<int>> v = // this will copy
_imageGItem->_faceSampler._facesframe.value(frame);
std::vector <cv::Rect_<int>>::const_iterator it = v.begin();
if( it != v.end())
{
qDebug()<<"here";
}
. Altri suggerimenti
QMap::value
restituisce il valore . che significa che è ritorno a copia del vettore, e ovviamente il tuo iteratore punta a un vettore diverso (una copia diversa).Modificalo per utilizzare invece la versione non cost operator[]
(come versione const
di quella anche ritorna per valore).O semplicemente utilizzare std::map
, che offre un'interfaccia molto migliore in questo senso.