Подтвердить что ты не робот

Преобразовать вектор int в вектор str

Я пытаюсь преобразовать vector<int> в vector<string>. Используя std::transform, я использовал std::to_string для преобразования int в string, но я продолжаю получать ошибку. Здесь мой код

#include <vector>
#include <iostream>
#include <algorithm>
#include <string>

int main(){
    std::vector<int> v_int;
    std::vector<std::string> v_str;

    for(int i = 0;i<5;++i)
        v_int.push_back(i);

    v_str.resize(v_int.size());
    std::transform(v_int.begin(),v_int.end(),v_str.begin(),std::to_string);
}

но я получаю эту ошибку

 no matching function for call to 'transform'
        std::transform(v_int.begin(),v_int.end(),v_str.begin(),std::to_string);
        ^~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1951:1: note: 
      candidate template ignored: couldn't infer template argument
      '_UnaryOperation'
transform(_InputIterator __first, _InputIterator __last, _OutputIterato...
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1961:1: note: 
      candidate function template not viable: requires 5 arguments, but 4 were
      provided
transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputItera...
4b9b3361

Ответ 1

std::to_string является перегруженной функцией, поэтому вам нужно предоставить листинг, чтобы устранить несоответствие

std::transform(v_int.begin(),v_int.end(),v_str.begin(),
               static_cast<std::string(*)(int)>(std::to_string));

Или используйте лямбда

std::transform(v_int.begin(),v_int.end(),v_str.begin(),
               [](int i){ return std::to_string(i); });