简体   繁体   English

Qt:QMap中向量的迭代器

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

I am working with Qt and OpenCV and I would like to create an iterator with std::vector <cv::Rect_<int>> to have access to all cv::Rect_. 我正在使用Qt和OpenCV,我想使用std::vector <cv::Rect_<int>>创建一个迭代器,以访问所有cv :: Rect_。
This vector is part of a QMap < int, std::vector <cv::Rect_<int>> > _facesframe; 此向量是QMap < int, std::vector <cv::Rect_<int>> > _facesframe;

So this is how I am trying to have access to these vectors: 所以这就是我试图访问这些向量的方式:

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

                }

But the program crashes at the line if... because of an incompatible iterator. 但是if...由于不兼容的迭代器,程序将崩溃。

Does someone know how to reach all cv::Rect_<int> of a QMap < int, std::vector <cv::Rect_<int>> > please? 有人知道如何到达QMap < int, std::vector <cv::Rect_<int>> > cv::Rect_<int>所有cv::Rect_<int>吗?

This is because you are comparing iterators to different vectors. 这是因为您正在将迭代器与不同的向量进行比较。

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

Vector is returned by value, so this is copied. 向量按值返回,因此将其复制。

You should use 你应该用

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

to correct this: 更正此:

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

  }

or (less efficient bacause of 1 copy being made): 或(由于制作一份副本的效率较低):

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 returns by value. QMap::value返回 Which means it's returning a copy of the vector, and of course your iterator points to a different vector (a different copy). 这意味着它正在返回向量的一个副本 ,当然,您的迭代器指向一个不同的向量(一个不同的副本)。 Change it to use the non-const version operator[] instead (as the const version of that also returns by value). 更改它以使用非const版本的operator[]代替(因为该const版本按值返回)。 Or just use std::map , which offers a much better interface in this regard. 或者只是使用std::map ,它在这方面提供了更好的界面。

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

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