How would one push back an empty vector of pairs to another vector?

pearbear picture pearbear · Apr 26, 2013 · Viewed 12.7k times · Source
  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?

Answer

Eric Undersander picture Eric Undersander · Apr 26, 2013

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() );