Hi
I'd like to iterate over a collection of objects and for each object in
the collection I'd like to invoke a specific method, see the following
code (I'd like to call the method m on each Item in the list)
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
struct Item {
m() { cout << "Item::m" << endl; }
};
typedef list<Item> ItemList;
int main() {
ItemList mylist;
mylist.push_back(Item());
mylist.push_back(Item());
mylist.push_back(Item());
foreach(mylist.begin(), mylist.end(), Item::m());
return 0;
}
However, this code does not work (I think it might work using some boost
libraries but I'd like it to work with only STL).
I guees there are some "adjustments" with some other STL cl*****, am I
right?
many thanks in advance
Lorenzo
P.S. of course I might write a for loop manually, but I'd like the more
compact foreach version :-)
--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it
MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com
http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen
http://doublecpp.sourceforge.net
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|