473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Trouble passing a function template into "for_each" algorithm

#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
Jul 22 '05 #1
3 1476
"Griff" <gr*****@aol.com> 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
Jul 22 '05 #2
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
Jul 22 '05 #3
>
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...
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: trash | last post by:
I do this kindda stuff often: class CVisitableObjectVector : public std::vector<Loki::BaseVisitable<>*> { void Enable(bool enable); .... };
11
by: franklini | last post by:
hello people, just wanted to say thanks again for the help in the past. i have a new problem which am wondering if any body can help me with. i have written this abtract class shape and its...
11
by: Tony Johansson | last post by:
Hello! I have some problem with STL function remove I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper...
17
by: silverburgh.meryl | last post by:
I have a function which calculate standard deviation. I am trying to re-write it using STL algorithm. I am thinking of using for_each() algorithm, but that will require my function passing in to...
6
by: Teddy.Gammell | last post by:
Hi, I would like to write a function which does this: for each element in the list call f1() of each element. if f1() return false, break the loop else continue I try to use the...
9
by: shaun | last post by:
I am working on code where I am handed a vector of pointers vector<T*> or a map<std::string, T*>, and I have to delete the objects and set the pointers to zero. I have been using a 'for' loop and...
27
by: Fraser Ross | last post by:
Is it wrong to describe for_each as a modifying algorithm? It is described as one here: http://www.josuttis.com/libbook/algolist.pdf. transform appears to be the algorithm to use for modifying...
35
by: Steven T. Hatton | last post by:
Perhaps I'm just a bit frustrated, and I will soon realize the clear truth of the matter, but right now I have some serious misgivings about the value of investing a lot of time and effort into...
13
by: Sarath | last post by:
What's the advantage of using for_each than the normal iteration using for loop? Is there any 'tweak' to use stream objects as 'Function' parameter of for_each loop. Seems copy function can do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.