Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 836
pearbear Avatar asked Apr 26 '13 00:04

pearbear


People also ask

How do you push an empty vector into a vector?

vector::push_back() push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1.

How do you push a pair into a vector?

The standard solution to add a new std::pair to a vector of pairs is using the std::emplace_back(T&&... args) function, which in-place construct and insert a pair at the end of a vector, using the specified arguments for its constructor. Note that this function is added in C++11.

How do you return an empty vector?

Explanation to the Code:Declare using std::cout and std::vector. Declare function with vector type return type which accepts vector as a variable. Then return null vector inside function body. Declare main function.

How do you push back a two dimensional vector?

So, If we have to push some small size vectors like 2 or 3 in 2-d vector then this way we can directly push in a 2-d vector i.e. vector_name. push_back({1,2,3}); likewise using {} brackets. Hence we don't have to use any extra temporary vector.


1 Answers

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() );
like image 125
Eric Undersander Avatar answered Sep 24 '22 06:09

Eric Undersander