### how to assign all items in vector std::vector<std::vector<cv::Point> > test to 0?

how to assign all items in vector std::vector<std::vector<cv::Point> > test to 0?
 ``123`` ``````std::vector > test ; // ... for( auto& pt : test ) pt = {} ;``````
Has the 2D vector already been sized and populated? If so loop through each dimension and zero out the individual elements.

Here's some code to illustrate how to deal with a 2D vector as a whole.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include #include template std::ostream& operator<<(std::ostream& os, const std::vector& v); template std::ostream& operator<<(std::ostream& os, const std::vector>& v); int main() { const size_t row_size { 3 }; const size_t col_size { 4 }; // create a sized 2D vector filled with a set value for all the elements. // 50 in this case. std::vector> a2DVec(row_size, std::vector(col_size, 50)); for (const auto& row : a2DVec) { for (const auto& col : row) { std::cout << col << ' '; } std::cout << '\n'; } std::cout << '\n'; const int zero { }; for (auto& row : a2DVec) { for (auto& col : row) { col = zero; } } /*for (const auto& row : a2DVec) { for (const auto& col : row) { std::cout << col << ' '; } std::cout << '\n'; }*/ // let's use a couple of templated std::ostream::operator<< overrides. // they make displaying a 1D and 2D vector as easy as plain old data // like an int or std::string std::cout << a2DVec; } template std::ostream& operator<<(std::ostream& os, const std::vector& v) { for (auto const& x : v) { os << x << ' '; } return os; } template std::ostream& operator<<(std::ostream& os, const std::vector>& v) { for (auto const& x : v) { os << x << '\n'; } return os; }``````
 ```50 50 50 50 50 50 50 50 50 50 50 50 0 0 0 0 0 0 0 0 0 0 0 0```

Changing all the elements in an already created 2D vector is easy.

I don't know what cv::Point is, and honestly don't bother telling me. :)

Creating a sized 2D vector from scratch with a set cv::Point value as default shouldn't be that hard. Just create a temp copy of your cv:Point object initialized to zero and have that value as col value instead of 50 in my example.
 JLBorges (13438) 1 2 3 std::vector > test ; // ... for( auto& pt : test ) pt = {} ;

Thank you!

Now I initialize matrix 100x10 with CV:Point like this:

std::vector<std::vector<cv::Point> > test
{
{{0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0}, {0 , 0} , {0 , 0} , {0 , 0}, {0 , 0} , {0 , 0} , {0 , 0}, {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0}, {0 , 0} , {0 , 0} , {0 , 0}, {0 , 0} , {0 , 0} , {0 , 0}} ,
{{0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0}, {0 , 0} , {0 , 0} , {0 , 0}, {0 , 0} , {0 , 0} , {0 , 0}, {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0} , {0 , 0}, {0 , 0} , {0 , 0} , {0 , 0}, {0 , 0} , {0 , 0} , {0 , 0}} ,

How should Initialize it right?
std::vector<std::vector<cv::Point>> test(100, {10, {0, 0}});
You would have found an answer more quickly by reading the documentation:
https://en.cppreference.com/w/cpp/container/vector/vector

Last edited on
Topic archived. No new replies allowed.