The reason your vector-of-vectors construction is not compiling is because
vector<vector<double>> b(a.begin(), a.end())
is range constructing b
with iterators whose element type is vector<int>
. This will attempt to construct b
's internal std::vector<double>
s with std::vector<int>
s, which is no surprise that it shouldn't compile.
Comparing this to your first example,
std::vector<float> v_float(v_int.begin(), v_int.end());
is range constructing v_float
from iterators whose element type is int
. This works because a conversion from an int to a float is allowed, but not from std::vector<int>
to std::vector<double>
(or std::vector<float>
).
You may want to instead look at looping through each element of a
and pushing back a vector constructed from a
's begin()
and end()