Cześć,
przykład:
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
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);
}
Błędy kompilatora:
prog.cpp: In function 'int main()':
prog.cpp:15:74: error: no matching function for call to 'transform(std::vector<int>::iterator, std::vector<int>::iterator, std::vector<std::basic_string<char> >::iterator, <unresolved overloaded function type>)'
std::transform(v_int.begin(),v_int.end(),v_str.begin(), std::to_string);
^
In file included from /usr/include/c++/5/algorithm:62:0,
from prog.cpp:3:
/usr/include/c++/5/bits/stl_algo.h:4164:5: note: candidate: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
transform(_InputIterator __first, _InputIterator __last,
^
/usr/include/c++/5/bits/stl_algo.h:4164:5: note: template argument deduction/substitution failed:
prog.cpp:15:74: note: couldn't deduce template parameter '_UnaryOperation'
std::transform(v_int.begin(),v_int.end(),v_str.begin(), std::to_string);
^
In file included from /usr/include/c++/5/algorithm:62:0,
from prog.cpp:3:
/usr/include/c++/5/bits/stl_algo.h:4201:5: note: candidate: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
transform(_InputIterator1 __first1, _InputIterator1 __last1,
^
/usr/include/c++/5/bits/stl_algo.h:4201:5: note: template argument deduction/substitution failed:
prog.cpp:15:74: note: candidate expects 5 arguments, 4 provided
std::transform(v_int.begin(),v_int.end(),v_str.begin(), std::to_string);
Link do ideone: http://ideone.com/M5pXD0
Pytanie: dlaczego kompilator nie potrafi wydedukować, której funkcji to_string użytć?
Skoro w wektorze używany jest int i jes on 'przekazywany' dalej do funkcji szablonowej transform, to dla chyba najlepsze byłoby wywołanie funkcji to_string(int).