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
Alan Turing Jun 18 '11 at 21:50 2011-06-18 21:50
source share2 answers
Use the range constructor std::vector :
std::vector<int> intVec; std::vector<double> doubleVec(intVec.begin(), intVec.end()); +102
James McNellis Jun 18 2018-11-18T00: 00Z
source shareAny 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
Jerry Zhang Jun 18 2018-11-18T00: 00Z
source share