Trouble passing a function template into "for_each" algorithm

G

Griff

#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
 
V

Victor Bazarov

Griff said:
#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() said:
}

=============================================================
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
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
 
G

Griff

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top