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?
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.
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.
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.
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.
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() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With