Click here to Skip to main content
15,899,679 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 3:15
Zac Howland13-Sep-06 3:15 
GeneralRe: STL Algorithms Pin
Kevin McFarlane13-Sep-06 4:11
Kevin McFarlane13-Sep-06 4:11 
AnswerRe: STL Algorithms Pin
Steve Echols12-Sep-06 21:29
Steve Echols12-Sep-06 21:29 
GeneralRe: STL Algorithms Pin
Stephen Hewitt12-Sep-06 22:27
Stephen Hewitt12-Sep-06 22:27 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 3:24
Zac Howland13-Sep-06 3:24 
GeneralRe: STL Algorithms Pin
Stephen Hewitt13-Sep-06 14:14
Stephen Hewitt13-Sep-06 14:14 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 18:32
Zac Howland13-Sep-06 18:32 
GeneralRe: STL Algorithms [modified] Pin
Stephen Hewitt13-Sep-06 19:32
Stephen Hewitt13-Sep-06 19:32 
Zac Howland wrote:
You don't have to write a functor to use for_each, nor most of the other algorithms. Using your own example (and by the way, your for_each example actually does nothing but waste CPU cycles as written), the following works perfectly fine:


Firstly in your example you've written a "one off" function instead of a "one off" functor, the same objection applies in this case.

Secondly, your code doesn't seem to work. Try compiling this:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
 
template <typename T>
T square_plus_one(const T& i)
{
    return (i * i + 1);
}
 
int main()
{
    // For notational convenience.
    using namespace std;
 
    vector<int> intVec;
 
    // Fill vector.
    for (int i=1; i<=10; ++i)
    {
       intVec.push_back(i);
    }
 
    // Transform the data.
    transform(intVec.begin(), intVec.end(), intVec.begin(), square_plus_one<int> );
 
    // Output the results.
    copy(intVec.begin(), intVec.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
 
    return 0;
}


I get the following error:
 "CommandLine.obj : error LNK2001: unresolved external symbol "int __cdecl square_plus_one(int const &)" (?square_plus_one@@YAHABH@Z)"
I'm not sure if this is a compiler bug or what (MSVC6) but regardless it's a problem.

As to the “wasted cycles” I concede that I made a mistake in that the results of my calculations are never used (oops). Functors are no less efficient in general however, consider the following. I've altered the code as follows:
// Changed function so we compile and made inline.
inline int square_plus_one(int i)
{
    return (i * i + 1);
}
 
// Added a functor version for comparison:
struct functor_square_plus_one : std::unary_function<int, int>
{
    int operator()(int i) const
    {
       return (i * i + 1);
    }
};
 
// Altered transform:
transform(intVec.begin(), intVec.end(), intVec.begin(), square_plus_one) ;
 
// Added call to functor version:
transform(intVec.begin(), intVec.end(), intVec.begin(), functor_square_plus_one()) ;



Below I show the machine code generated in a release build using MSVC6 for each call to 'transform':

// Using your method code is 0x2A bytes long.
//  transform(intVec.begin(), intVec.end(), intVec.begin(), square_plus_one);
004010DF   mov         ecx,dword ptr [esp+14h]
004010E3   mov         edx,dword ptr [esp+18h]
004010E7   mov         eax,ecx
004010E9   push        edi
004010EA   cmp         eax,edx
004010EC   mov         esi,edx
004010EE   je          main+89h (00401109)
004010F0   mov         edx,dword ptr [eax]
004010F2   add         eax,4
004010F5   mov         edi,edx
004010F7   add         ecx,4
004010FA   imul        edi,edx
004010FD   inc         edi
004010FE   cmp         eax,esi
00401100   mov         dword ptr [ecx-4],edi
00401103   jne         main+70h (004010f0)
00401105   mov         edx,dword ptr [esp+1Ch]
00401109
 
// Using my method code is identical.
//  transform(intVec.begin(), intVec.end(), intVec.begin(), functor_square_plus_one()) ;
004010DF   mov         ecx,dword ptr [esp+14h]
004010E3   mov         edx,dword ptr [esp+18h]
004010E7   mov         eax,ecx
004010E9   push        edi
004010EA   cmp         eax,edx
004010EC   mov         esi,edx
004010EE   je          main+89h (00401109)
004010F0   mov         edx,dword ptr [eax]
004010F2   add         eax,4
004010F5   mov         edi,edx
004010F7   add         ecx,4
004010FA   imul        edi,edx
004010FD   inc         edi
004010FE   cmp         eax,esi
00401100   mov         dword ptr [ecx-4],edi
00401103   jne         main+70h (004010f0)
00401105   mov         edx,dword ptr [esp+1Ch]
00401109


NOTE: I actually compiled both methods with the other commented out to avoid any possibility of the compiler using using cached values from the one before. That's why both versions start at the same address.

There is no waste of "cycles" or space. The code is byte for byte identical. From what I hear from experts there are cases in which the functor version is actually more efficient as many compilers find it easier to inline a functor then code via a function pointer.


Zac Howland wrote:
What I meant is that the containers are not completely interchangible


Of course not. That doesn't negate the general principle.



Steve

GeneralRe: STL Algorithms Pin
Zac Howland14-Sep-06 3:13
Zac Howland14-Sep-06 3:13 
GeneralRe: STL Algorithms Pin
Stephen Hewitt14-Sep-06 13:50
Stephen Hewitt14-Sep-06 13:50 
GeneralRe: STL Algorithms Pin
Zac Howland14-Sep-06 19:17
Zac Howland14-Sep-06 19:17 
GeneralRe: STL Algorithms Pin
Stephen Hewitt14-Sep-06 19:27
Stephen Hewitt14-Sep-06 19:27 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 3:21
Zac Howland13-Sep-06 3:21 
AnswerRe: STL Algorithms Pin
Kevin McFarlane13-Sep-06 4:08
Kevin McFarlane13-Sep-06 4:08 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 4:23
Zac Howland13-Sep-06 4:23 
GeneralRe: STL Algorithms Pin
Kevin McFarlane13-Sep-06 9:01
Kevin McFarlane13-Sep-06 9:01 
GeneralRe: STL Algorithms Pin
Zac Howland13-Sep-06 9:17
Zac Howland13-Sep-06 9:17 
AnswerRe: STL Algorithms Pin
Stephen Hewitt13-Sep-06 22:52
Stephen Hewitt13-Sep-06 22:52 
GeneralRe: STL Algorithms Pin
Rob Caldecott13-Sep-06 23:40
Rob Caldecott13-Sep-06 23:40 
GeneralRe: STL Algorithms Pin
Stuart Dootson14-Sep-06 1:17
professionalStuart Dootson14-Sep-06 1:17 
GeneralRe: STL Algorithms Pin
Zac Howland14-Sep-06 3:17
Zac Howland14-Sep-06 3:17 
GeneralRe: STL Algorithms Pin
Nemanja Trifunovic15-Sep-06 4:43
Nemanja Trifunovic15-Sep-06 4:43 
GeneralRe: STL Algorithms Pin
Zac Howland15-Sep-06 5:26
Zac Howland15-Sep-06 5:26 
GeneralRe: STL Algorithms Pin
Stuart Dootson15-Sep-06 5:43
professionalStuart Dootson15-Sep-06 5:43 
GeneralRe: STL Algorithms Pin
Rob Caldecott17-Sep-06 23:49
Rob Caldecott17-Sep-06 23:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.