Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > iterating over ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 9275 of 10094
Post > Topic >>

iterating over a collection with foreach

by Lorenzo Bettini <bettini@[EMAIL PROTECTED] > Feb 7, 2008 at 12:25 PM

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! ]
 




 4 Posts in Topic:
iterating over a collection with foreach
Lorenzo Bettini <betti  2008-02-07 12:25:52 
Re: iterating over a collection with foreach
red floyd <no.spam@[EM  2008-02-07 23:12:04 
Re: iterating over a collection with foreach
Carl Barron <cbarron41  2008-02-07 23:12:40 
Re: iterating over a collection with foreach
Alberto Ganesh Barbati &l  2008-02-07 23:12:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 8:08:58 CDT 2008.