提问者:小点点

C++在向unordered_map中插入对的向量时缺少元素


null

/**
 * @typedef PixelPos
 * @brief   Coordinates of pixel position
*/
using PixelPos = pair<int, int>; /// (row,column)
using ContextTableBase = unordered_map< PixelPos, vector<PixelPos> >; /// (hash(px), list of neighbours)

为了方便起见,为了保留实现散列函数所必需的信息,而不是直接使用unordered_map,定义了一个新的子类。

class ContextTable : public ContextTableBase
{
///... definition of operators and constructor ...
               return ContextTableBase::operator[]( PixelPos{ oPixel.first * (height - (height-width)), oPixel.second } );
} 

null

template<> struct hash<PixelPos>
    {
        std::size_t operator()(PixelPos const& pixel) const noexcept
        {
            return pixel.first + pixel.second; /// mapped row + col
        }
    };

null

/**
 * @brief Función local para aplicar la mascara del contexto local a cada uno de los pixeles
 * @param oPosition [in] - px location
 * @param oMask [in] - context mask
 * @param width [in] - number of columns of the image
 * @param height [in] - number of rows of the image
 * @param oRet [out] - Return list
 * @returns oRet
*/
static void applyMask(const PixelPos& oPosition, const ContextMask& oMask, const int width, const int height, vector<PixelPos>& oRet)
{
    for(const auto& dot : oMask )
    {
        PixelPos oCandidate{ oPosition.first + dot.first, oPosition.second + dot.second };
/// ... do something to check if candidate is neighbour...
            oRet.push_back(oCandidate);
/// ... do something else ...
    }
}

/// ... more code ...

/**
 * @brief Context calculator
 * @param N [in] - Context size
 * @param width [in] - number of columns of the image
 * @param height [in] - number of row of the image
 * @returns oTable, hash table which entries are pxs and returns a list of neighbours
*/
ContextTable& getLocalContext(const size_t N, const int width, const int height)
{

    ContextTable oTable = new ContextTable(height, width);
    ContextMask oMask{ createPixelMask(N) };

    for(int row = 0; row < height; ++row)
    {
        for(int col = 0; col < width; ++col)
        {

            PixelPos oPosition{ row, col };
            if( oTable.find(oPosition) == oTable.end() )
            {
                vector<PixelPos> oNeighbours{};
                applyMask(oPosition, oMask, width, height, oNeighbours);
                oTable.insert(make_pair(oPosition, oNeighbours));
            }
                

        }
    }
  return (*oTable);
}

这个函数返回的向量是正确的,并且正好有我要找的像素,但是在将它添加到oTable之后,在离开getLocalContext之前,第一行下面的像素条目会丢失。

作为一个例子,显示了大小为5x4(行,列)的图像的问题输出

要插入到oTable中的正确值(取自每次调用applyMask后的oNeighbours)是

applyMask px central: r: 0| c: 0
applyMask px central: r: 0| c: 1
applyMask    n: r: 0| c: 0
applyMask px central: r: 0| c: 2
applyMask    n: r: 0| c: 1
applyMask    n: r: 0| c: 0
applyMask px central: r: 0| c: 3
applyMask    n: r: 0| c: 2
applyMask    n: r: 0| c: 1
applyMask px central: r: 1| c: 0
applyMask    n: r: 0| c: 0
applyMask    n: r: 0| c: 1
applyMask px central: r: 1| c: 1
applyMask    n: r: 1| c: 0
applyMask    n: r: 0| c: 1
applyMask    n: r: 0| c: 0
applyMask    n: r: 0| c: 2
applyMask px central: r: 1| c: 2
applyMask    n: r: 1| c: 1
applyMask    n: r: 0| c: 2
applyMask    n: r: 0| c: 1
applyMask    n: r: 0| c: 3
applyMask    n: r: 1| c: 0
applyMask px central: r: 1| c: 3
applyMask    n: r: 1| c: 2
applyMask    n: r: 0| c: 3
applyMask    n: r: 0| c: 2
applyMask    n: r: 1| c: 1
applyMask px central: r: 2| c: 0
applyMask    n: r: 1| c: 0
applyMask    n: r: 1| c: 1
applyMask px central: r: 2| c: 1
applyMask    n: r: 2| c: 0
applyMask    n: r: 1| c: 1
applyMask    n: r: 1| c: 0
applyMask    n: r: 1| c: 2
applyMask px central: r: 2| c: 2
applyMask    n: r: 2| c: 1
applyMask    n: r: 1| c: 2
applyMask    n: r: 1| c: 1
applyMask    n: r: 1| c: 3
applyMask    n: r: 2| c: 0
applyMask px central: r: 2| c: 3
applyMask    n: r: 2| c: 2
applyMask    n: r: 1| c: 3
applyMask    n: r: 1| c: 2
applyMask    n: r: 2| c: 1
applyMask px central: r: 3| c: 0
applyMask    n: r: 2| c: 0
applyMask    n: r: 2| c: 1
applyMask px central: r: 3| c: 1
applyMask    n: r: 3| c: 0
applyMask    n: r: 2| c: 1
applyMask    n: r: 2| c: 0
applyMask    n: r: 2| c: 2
applyMask px central: r: 3| c: 2
applyMask    n: r: 3| c: 1
applyMask    n: r: 2| c: 2
applyMask    n: r: 2| c: 1
applyMask    n: r: 2| c: 3
applyMask    n: r: 3| c: 0
applyMask px central: r: 3| c: 3
applyMask    n: r: 3| c: 2
applyMask    n: r: 2| c: 3
applyMask    n: r: 2| c: 2
applyMask    n: r: 3| c: 1
applyMask px central: r: 4| c: 0
applyMask    n: r: 3| c: 0
applyMask    n: r: 3| c: 1
applyMask px central: r: 4| c: 1
applyMask    n: r: 4| c: 0
applyMask    n: r: 3| c: 1
applyMask    n: r: 3| c: 0
applyMask    n: r: 3| c: 2
applyMask px central: r: 4| c: 2
applyMask    n: r: 4| c: 1
applyMask    n: r: 3| c: 2
applyMask    n: r: 3| c: 1
applyMask    n: r: 3| c: 3
applyMask    n: r: 4| c: 0
applyMask px central: r: 4| c: 3
applyMask    n: r: 4| c: 2
applyMask    n: r: 3| c: 3
applyMask    n: r: 3| c: 2
applyMask    n: r: 4| c: 1

null

 px central: r: 0| c: 0
 px central: r: 0| c: 1
     n: r: 0| c: 0
 px central: r: 0| c: 2
     n: r: 0| c: 1
     n: r: 0| c: 0
 px central: r: 0| c: 3
     n: r: 0| c: 2
     n: r: 0| c: 1
 px central: r: 1| c: 0
     n: r: 3| c: 0
     n: r: 3| c: 1
 px central: r: 1| c: 1
     n: r: 4| c: 0
     n: r: 3| c: 1
     n: r: 3| c: 0
     n: r: 3| c: 2
 px central: r: 1| c: 2
     n: r: 4| c: 1
     n: r: 3| c: 2
     n: r: 3| c: 1
     n: r: 3| c: 3
     n: r: 4| c: 0
 px central: r: 1| c: 3
     n: r: 4| c: 2
     n: r: 3| c: 3
     n: r: 3| c: 2
     n: r: 4| c: 1
 px central: r: 2| c: 0
 px central: r: 2| c: 1
 px central: r: 2| c: 2
 px central: r: 2| c: 3
 px central: r: 3| c: 0
 px central: r: 3| c: 1
 px central: r: 3| c: 2
 px central: r: 3| c: 3
 px central: r: 4| c: 0
 px central: r: 4| c: 1
 px central: r: 4| c: 2
 px central: r: 4| c: 3

共1个答案

匿名用户

在意识到unordered_map的find和insert方法没有被重写,从而调用了错误版本的哈希函数之后,解决了这个问题。

对getLocalContext应用的更改为

       PixelPos oMappedPosition{ row * (height - (height-width)), col };
       if( oTable.find(oMappedPosition) == oTable.end() )
       {
         vector<PixelPos> oNeighbours{};
         applyMask(PixelPos{row, col}, oMask, width, height, oNeighbours);
         oTable.insert(make_pair(oMappedPosition, oNeighbours));
       }

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|unordered_map|中|插入|向量|缺少|元素)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?