C ++ convert vector <int> to vector <double>

What is a good clean way to convert std::vector<int> intVec to std::vector<double> doubleVec . Or, more generally, convert two vectors of convertible types?

+57
c ++ type-conversion vector stl
Jun 18 '11 at 21:50
source share
2 answers

Use the range constructor std::vector :

 std::vector<int> intVec; std::vector<double> doubleVec(intVec.begin(), intVec.end()); 
+102
Jun 18 2018-11-18T00:
source share

Any problem? I can work on my pc. My IDE is Code :: blocks.

 #include <iostream> #include <vector> #include <iterator> using namespace std; int main() { vector<int> v_int; for (int i=0; i<10; ++i) { v_int.push_back(i); } vector<double> v_float(v_int.begin(), v_int.end()); copy (v_float.begin(), v_float.end(), ostream_iterator<double>(cout, " ")); cout << endl; return 0; } 
+3
Jun 18 2018-11-18T00:
source share



All Articles