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?
1
2
3
std::vector<std::vector<cv::Point> > 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& 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<std::vector<int>> a2DVec(row_size, std::vector<int>(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 <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
   for (auto const& x : v)
   {
      os << x << ' ';
   }

   return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& 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<std::vector<cv::Point> > 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.