14
  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> >  );
  }

This is my code, but I am getting the errors:

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

I do not want any values in the pairs, I just would like to have a vector of empty vectors at the moment. How would I do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
pearbear
  • 1,025
  • 4
  • 18
  • 23

2 Answers2

18

You want to construct a vector to pass to push_back and you're just missing parentheses:

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

Or, instead of your loop, you could just do the following. It's better because the vector will allocate just the right amount of memory in a single allocation:

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

Or this, which is more concise because it's using resize's default 2nd argument:

offset_table.resize( offset.Width()*offset.Width() );
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.
    }
}
pcbabu
  • 2,219
  • 4
  • 22
  • 32
  • I think you've misunderstood the question. I think the question was how do you push_back an empty vector to a vector of vectors. – Owl Jul 24 '16 at 23:01