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

C++StlVectorType Conversion

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

Use std::vector's range constructor:

std::vector<int> intVec;
std::vector<double> doubleVec(intVec.begin(), intVec.end());

Solution 2 - C++

Use std::transform algorithm:

std::transform(intVec.begin(), intVec.end(), doubleVec.begin(), [](int x) { return (double)x;});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlan TuringView Question on Stackoverflow
Solution 1 - C++James McNellisView Answer on Stackoverflow
Solution 2 - C++YasView Answer on Stackoverflow