繁体   English   中英

Qt:QMap中向量的迭代器

[英]Qt: iterator of a vector in a QMap

我正在使用Qt和OpenCV,我想使用std::vector <cv::Rect_<int>>创建一个迭代器,以访问所有cv :: Rect_。
此向量是QMap < int, std::vector <cv::Rect_<int>> > _facesframe;

所以这就是我试图访问这些向量的方式:

                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";
                    }

                }

但是if...由于不兼容的迭代器,程序将崩溃。

有人知道如何到达QMap < int, std::vector <cv::Rect_<int>> > cv::Rect_<int>所有cv::Rect_<int>吗?

这是因为您正在将迭代器与不同的向量进行比较。

const T QMap::value(const Key & key, const T & defaultValue = T()) const

向量按值返回,因此将其复制。

你应该用

T & QMap::operator[](const Key & key)

更正此:

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";
      }

  }

或(由于制作一份副本的效率较低):

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";
  }

QMap::value返回 这意味着它正在返回向量的一个副本 ,当然,您的迭代器指向一个不同的向量(一个不同的副本)。 更改它以使用非const版本的operator[]代替(因为该const版本按值返回)。 或者只是使用std::map ,它在这方面提供了更好的界面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM