C++ Konwertuj wektor na wektor

Jaki jest dobry czysty sposób na konwersję std::vector<int> intVec na std::vector<double> doubleVec. Albo, ogólniej, do konwersji dwóch wektorów typów wymiennych?

Author: Alan Turing, 2011-06-19

2 answers

Użyj konstruktora zakresustd::vector:

std::vector<int> intVec;
std::vector<double> doubleVec(intVec.begin(), intVec.end());
 126
Author: James McNellis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-30 13:00:33

Jakiś problem? Mogę uruchomić na moim komputerze. Mój IDE to 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
Author: Jerry Zhang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-18 23:06:13