#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 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
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
> 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... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: trash |
last post by:
I do this kindda stuff often:
class CVisitableObjectVector : public
std::vector<Loki::BaseVisitable<>*> {
void Enable(bool enable);
....
};
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |