Trouble passing a function template into "for_each" algorithm

Discussion in 'C++' started by Griff, Jul 10, 2004.

  1. Griff

    Griff Guest

    #include <iostream>
    using namespace std;

    #include <vector>
    #include <string>
    #include <fstream>
    #include <algorithm>

    template<class C>void PrintAll(C&v)
    {
    typename C::iterator i;
    for (i = v.begin(); i!= v.end(); ++i)
    {
    cout << *i << endl;
    }
    }

    void main()
    {
    const string s1 = "First line";
    const string s2 = "Second line";
    const string s3 = "Third line";

    vector<string> V;

    V.push_back(s1); V.push_back(s2); V.push_back(s3);

    for_each(V.begin(),V.end(),PrintAll);
    }

    =============================================================
    I won't bore you (yet) with the reams of complaint from MS VC++ when I
    try to compile the above...

    My question is:

    What is the correct syntax to use "for_each" to call the function
    template PrintAll ?

    for_each(V.begin(),V.end(),PrintAll<const string>); ??
    for_each(V.begin(),V.end(),PrintAll); ??

    or is it even possible ?

    Assistance and explanation would be welcomed on this.

    Best regards,

    Griff
    Griff, Jul 10, 2004
    #1
    1. Advertising

  2. "Griff" <> wrote...
    > #include <iostream>
    > using namespace std;
    >
    > #include <vector>
    > #include <string>
    > #include <fstream>
    > #include <algorithm>
    >
    > template<class C>void PrintAll(C&v)
    > {
    > typename C::iterator i;
    > for (i = v.begin(); i!= v.end(); ++i)
    > {
    > cout << *i << endl;
    > }
    > }
    >
    > void main()


    int main()

    > {
    > const string s1 = "First line";
    > const string s2 = "Second line";
    > const string s3 = "Third line";
    >
    > vector<string> V;
    >
    > V.push_back(s1); V.push_back(s2); V.push_back(s3);
    >
    > for_each(V.begin(),V.end(),PrintAll);


    for_each(V.begin(), V.end(), PrintAll<string>);

    > }
    >
    > =============================================================
    > I won't bore you (yet) with the reams of complaint from MS VC++ when I
    > try to compile the above...
    >
    > My question is:
    >
    > What is the correct syntax to use "for_each" to call the function
    > template PrintAll ?
    >
    > for_each(V.begin(),V.end(),PrintAll<const string>); ??
    > for_each(V.begin(),V.end(),PrintAll); ??


    Since 'PrintAll' is a template, and 'for_each' needs a function or
    a functor, you need to _instantiate_ it: PrintAll<string>.

    >
    > or is it even possible ?


    Of course it is.

    C++ Standard library's "string" class conforms to the Standard Container
    requirements and that means it has the "iterator" type defined, has
    "begin" and "end" members, so you can use it to iterate the characters
    in it.

    As to Visual C++, I don't know whether 6.0 will be able to compile this,
    but I sincerely hope that 7.1 will.

    Victor
    Victor Bazarov, Jul 10, 2004
    #2
    1. Advertising

  3. Griff

    Griff Guest

    Victor,

    Thanks for that. Your solution was pretty much what I tried, but VC++
    6.0 chokes on it. Glad to have independent confirmation that the
    problem is with MS,
    and not my understanding of the code (for once).

    I'll try it again at work on monday, when I can use a decent system.
    (Solaris and g++).

    Cheers

    - Griff
    Griff, Jul 11, 2004
    #3
  4. Griff

    Griff Guest

    >
    > I'll try it again at work on monday, when I can use a decent system.
    > (Solaris and g++).
    >


    Yep, works beautifully on Solaris 8 with gcc 3.2...
    Griff, Jul 12, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    544
    PJP of NYC
    May 24, 2005
  2. Replies:
    17
    Views:
    1,242
    Daniel T.
    Jan 13, 2006
  3. shaun
    Replies:
    9
    Views:
    307
    shaun roe
    Jun 10, 2006
  4. mthread
    Replies:
    4
    Views:
    448
    James Kanze
    Jun 1, 2009
  5. , India
    Replies:
    2
    Views:
    1,975
    James Kanze
    Nov 15, 2009
Loading...

Share This Page