Thread: elements in std::set

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    elements in std::set

    Hello

    I have a simple
    Code:
    std::set<object> someset;
    Where object is:

    Code:
    class object {
    public:
      void some_function() { }
    };
    How can I call some_function() for all elements in set container with STD algorithms within one line?

    Many thanks in advance!

  2. #2

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Code:
    std::for_each( someset.begin(), someset.end(),
                   boost::bind( &someset::some_function, _1 ) );

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks a lot!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    FYI, the non-boost way (I believe?) would be:
    Code:
    std::for_each(someset.begin(),someset.end(),std::mem_fun_ref(&object::some_function));
    I could be a little off there...

    Quote Originally Posted by pheres
    Code:
    std::for_each( someset.begin(), someset.end(),
                   boost::bind( &someset::some_function, _1 ) );
    I've never used boost before, but are you sure that's right? someset is a set container so does &someset::some_function make sense if some_function is a member function of object? It just looks a little off.
    Last edited by hk_mp5kpdw; 10-08-2008 at 06:21 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    ups, you are right:

    Code:
    std::for_each( someset.begin(), someset.end(),
                   boost::bind( &object::some_function, _1 ) );
    does it work?

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    use mem_fun or mem_fun_ref with for_each:
    Code:
    for_each(container.begin(), container.end(), mem_fun_ref( &object::some_function )  )
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by King Mir View Post
    use mem_fun or mem_fun_ref with for_each:
    Code:
    for_each(container.begin(), container.end(), mem_fun_ref( &object::some_function )  )
    Can you pass function arguments with mem_fun ?

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Let's say that some_function was a member function that accepted an integer as an argument... you could then do (as an example):
    Code:
    std::for_each(someset.begin(),someset.end(),std::bind2nd(std::mem_fun_ref(&object::some_function),4));
    Which would effectively call the member function some_function(4) for each object in container someset... if I understand what you're asking.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  2. How to access a dynamic array inside a std::set ?
    By IndioDoido in forum C++ Programming
    Replies: 16
    Last Post: 11-04-2007, 05:27 PM
  3. Randomly rearranging the elements in a vector
    By Signifier in forum C++ Programming
    Replies: 11
    Last Post: 08-01-2007, 12:21 PM
  4. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 11:30 AM
  5. Replies: 2
    Last Post: 08-03-2003, 10:01 AM