[C++] huidig iterator element als argument voor predicate

Pagina: 1
Acties:

  • Amotea
  • Registratie: Mei 2004
  • Laatst online: 23-01 17:45
Is er een standaard manier om, in STL functies die een predicate of andere functie toepassen op ieder element bij een iteratie (zoals bijvoorbeeld de functie std::count_if), het huidige iterator element door te geven als parameter aan de predicate als dit een memberfunctie is van een andere class?
Ter verheldering:
Section 18.4.4.2 of The C++ Programming Language describes a technique "Member Function Adapters" for calling the same member function for each object in a container. The C++ STL provides several functions for this purpose. However, the STL does not provide for calling the member of another object outside of the container using the contained objects as parameters.
Op bovenstaande website staat een oplossing beschreven en de implementatie van deze oplossing werkt ook prima, maar ik kan me niet voorstellen dat er geen standaard oplossing voor dit probleem is (wellicht in de Boost library?).

Code van bovenstaande website maakt het dus mogelijk om het volgende te doen:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  template <class Type> class basic_accumulator {
public:
  basic_accumulator(void) { m_typ = 0; };
  bool accumulate(Type i_typ) { m_typ += i_typ; };
  Type results(void) { return m_typ; };
protected:
  Type m_typ;
};
typedef basic_accumulator<int> accumulator;

int main()
{
    accumulator a; // dit is dus de user defined class waarvan een member aangeroepen gaat worden voor elk element uit de iterator
    vector<int> v;

    v.push_back(1);
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
}

  for_each(v.begin(),v.end(),far_mem_fun(a,&accumulator::accumulate));

Vraag: weet iemand een oplossing voor bovenstaand probleem, die ondersteunt wordt door de standaard of Boost library? (en dan bedoel ik dus niet een global functie maken die een class gebruikt die global is gedeclareerd)

  • codeneos
  • Registratie: Augustus 2004
  • Laatst online: 01-12 01:30

codeneos

[...]

Je kan toch ook gewoon een for loop gebruiken (op de vector)? En dus niet de for_each functie.

[ Voor 8% gewijzigd door codeneos op 12-07-2007 15:42 ]

http://www.tweakers.net/gallery/119170/sys


  • .oisyn
  • Registratie: September 2000
  • Laatst online: 23:31

.oisyn

Moderator Devschuur®

Demotivational Speaker

std::bind1st(std::mem_fun(&accumulator::accumulate), &a);

boost kan het uiteraard ook, met boost::bind en boost::mem_fn. Die zijn wat uitgebreider dan hun std tegenhanger (hoewel je boost::mem_fn niet eens nodig hebt, boost::bind snapt member functie pointers automatisch: boost::bind(&accumulator::accumulate, &a))

[ Voor 51% gewijzigd door .oisyn op 12-07-2007 15:51 ]

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • Amotea
  • Registratie: Mei 2004
  • Laatst online: 23-01 17:45
Bedankt, ik vermoedde al dat het een combinatie van een bind en mem_fun was, maar kon niet precies zien wat het moest zijn.

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 23:31

.oisyn

Moderator Devschuur®

Demotivational Speaker

Overigens heb je voor dit voorbeeld ook gewoon simpelweg std::accumulate(v.begin(), v.end()) ;)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.