简体   繁体   中英

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

  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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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