V
Vijai Kalyan
Hello,
I am trying to use std::transform to take in a collection of strings,
transform them and insert the result into an output container. So, I
wrote something like this:
std::wstring doSomething (std::wstring const& str)
{
// do something to copy of str and return new value
}
void foo ()
{
std::set<std::wstring> input;
std::set<std::wstring> output;
std::transform (input.begin(), input.end(), output.begin(),
doSomething);
}
I stepped into the implementation of std::transform (on my platform)
and it goes something like:-
template<class _InIt,
class _OutIt,
class _Fn1> inline
_OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func)
{ // transform [_First, _Last) with _Func
for (; _First != _Last; ++_First, ++_Dest)
*_Dest = _Func(*_First);
return (_Dest);
}
It looks like the
*_Dest = _Func (*_First)
would mean that transform expects the output objects to already exist.
Is that right? I keep crashing at the assignment. Does std::transform
require sequence containers with pre-reserved sizes?
thanks,
-vijai.
I am trying to use std::transform to take in a collection of strings,
transform them and insert the result into an output container. So, I
wrote something like this:
std::wstring doSomething (std::wstring const& str)
{
// do something to copy of str and return new value
}
void foo ()
{
std::set<std::wstring> input;
std::set<std::wstring> output;
std::transform (input.begin(), input.end(), output.begin(),
doSomething);
}
I stepped into the implementation of std::transform (on my platform)
and it goes something like:-
template<class _InIt,
class _OutIt,
class _Fn1> inline
_OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func)
{ // transform [_First, _Last) with _Func
for (; _First != _Last; ++_First, ++_Dest)
*_Dest = _Func(*_First);
return (_Dest);
}
It looks like the
*_Dest = _Func (*_First)
would mean that transform expects the output objects to already exist.
Is that right? I keep crashing at the assignment. Does std::transform
require sequence containers with pre-reserved sizes?
thanks,
-vijai.