0

This is what I have:

vector<int> A {5,4,3,1};
vector<int> B {3,1};

I want make 2d vector C to 5*5 2d vector filled in 0.0 so I tried this:

vector<vector<double>> C((A.size() + 1), vector<double>((B.begin() + 2),0.0));

But this is an error I'm getting.

There is no instance of a constructor with a matching C++ argument list.
            (std::_Vector_iterator<std::_Vector_val<std::conditional_t<true, std::_Simple_types<int>, std::_Vec_iter_types<int, size_t, ptrdiff_t, int *, const int *, int &, const int &>>>>, double)

I can't understand the content of the error. May I ask what is wrong and how to fix it?

Marcin Poloczek
  • 923
  • 6
  • 21
lampseeker
  • 37
  • 6
  • What is exactly the content of the vector `C` that you want to obtain? – Damien Apr 08 '21 at 14:13
  • If you want a 5x5 2d vector of `double`s (you seem to want to initialize with `0.0`): `vector> C(5, vector(5, 0.0));` (where the `0.0` is the same as the default you'd get if you left that argument out) – Ted Lyngmo Apr 08 '21 at 14:14
  • Does this answer your question? [Vector of Vectors to create matrix](https://stackoverflow.com/questions/12375591/vector-of-vectors-to-create-matrix) – Marcin Poloczek Apr 08 '21 at 14:39

1 Answers1

0
vector<vector<double>> C((A.size() + 1), vector<double>((B.begin() + 2),0.0));

Here you are invoking this constructor of std::vector (number 3 on cppreference):

constexpr vector( size_type count,
                  const T& value,
                  const Allocator& alloc = Allocator());

You can see that it takes in size and value. In your nested vector instead of passing size you are passing iterator the the beginning and increment it by two. I think what you've wanted is B.size() instead of B.begin(). Also if it has to be 5 x 5 you can simply do this:

std::vector<std::vector<double>> C(5, std::vector<double>(5, 0.0));
Marcin Poloczek
  • 923
  • 6
  • 21
  • Thanks for the kind explanation! so, I change vector> C((A.size() + 1), vector((B.begin() + 2),0.0)); to C((A.size() + 1), vector((B.front() + 2),0.0)); that is work. I will study about iterator more... – lampseeker Apr 08 '21 at 15:06