12
  std::vector<std::vector< std::pair<int, int> > > offset_table;
  for (int i = 0; i < (offset.Width()*offset.Width()); ++i)
  {
    offset_table.push_back(  std::vector< std::pair<int, int> >  );
  }

这是我的代码,但我收到错误:

main.cpp: In function ‘void Compress(const Image<Color>&, Image<bool>&, Image<Color>&, Image<Offset>&)’:
main.cpp:48:66: error: expected primary-expression before ‘)’ token

我不想要成对中的任何值,我现在只想有一个空向量的向量。我该怎么做?

4

2 回答 2

16

您想构造一个向量以传递给 push_back ,而您只是缺少括号:

offset_table.push_back(  std::vector< std::pair<int, int> >()  );

或者,您可以执行以下操作,而不是您的循环。更好,因为向量将在一次分配中分配适量的内存:

offset_table.resize( offset.Width()*offset.Width(), std::vector< std::pair<int, int> >() );

或者这个,更简洁,因为它使用调整大小的默认第二个参数:

offset_table.resize( offset.Width()*offset.Width() );
于 2013-04-26T08:10:12.257 回答
0
std::vector<std::vector< std::pair<int, int> > > offset_table;

This is is a 2d array so you need to use nested array. For only getting the length if inner vector.

for(vector< pair<int, int >> vv in offset_table)
{
    if(vv.size() == 0)
    {
        // this is your target.
    }
}
于 2013-04-26T10:31:15.623 回答