GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Aug-2008, 15:33
noxious noxious is offline
New Member
 
Join Date: Aug 2008
Posts: 4
noxious is on a distinguished road

Using for_each in List


I need a simple code that operates one list, and uses the for_each() function, like i got one list of elements 1,2,3,4,5,6,7,8,9 and the other one should be the list that consist of every element from the first + his left element like:

1 2 3 4 5 6 7 8 9 - original
10 3 5 7 9 ... - modified ( note that first and last are summed)


the code i have for something like beginning or just an example is i think is like...

CPP / C++ / C Code:

    #include "algostuff.hpp"
    using namespace std;


    // function object that adds the value with which it is initialized
    template <c1*** T>
    c1*** AddValue {
      private:
        T theValue;    // value to add
      public:
        // constructor initializes the value to add
        AddValue (const T& v) : theValue(v) {
        }

        // the function call for the element adds the value
        void operator() (T& elem) const {
            elem += theValue;
        }
    };

    int main()
    {

        vector<int> coll;

        INSERT_ELEMENTS(coll,1,9);

[color="Red"]        // add ten to each element
        for_each (coll.begin(), coll.end(),         // range
                  AddValue<int>(10));               // operation
        PRINT_ELEMENTS(coll);

        // add value of first element to each element
        for_each (coll.begin(), coll.end(),         // range
                  AddValue<int>(*coll.begin()));    // operation
        PRINT_ELEMENTS(coll);[/color]
    }

The AddValue<> c1*** defines function objects that add a value to each element that is passed to the constructor. Using the function object has the advantage that you can process the added value at runtime. The program has the following output:

11 12 13 14 15 16 17 18 19
22 23 24 25 26 27 28 29 30
Last edited by admin : 12-Aug-2008 at 19:33. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 12-Aug-2008, 16:53
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: using for_each in List


There is an example on this page.
  #3  
Old 13-Aug-2008, 10:20
noxious noxious is offline
New Member
 
Join Date: Aug 2008
Posts: 4
noxious is on a distinguished road

Re: using for_each in List


thank you fakepoo i really apreciate yor help. But here is used function, so can you help me how to made that function so the new list schould be like the examples given to my first post. I don't know how can I add each predecessor element value to the new, like this:
1 2 3 4 5 6 original list
7 3 5 7 11 new list
(the first and last element are summed)

that schould be a function but i still don't know how to program it

I REALLY THANK ALL HELP
  #4  
Old 13-Aug-2008, 10:21
noxious noxious is offline
New Member
 
Join Date: Aug 2008
Posts: 4
noxious is on a distinguished road

Re: using for_each in List


Quote:
Originally Posted by fakepoo
There is an example on www.cplusplus.com.


please read my quick reply :S
  #5  
Old 13-Aug-2008, 13:06
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Using for_each in List


Sorry, I'm not really sure how to access other elements of the data structure from within a function that only looks at one element.
  #6  
Old 14-Aug-2008, 08:47
noxious noxious is offline
New Member
 
Join Date: Aug 2008
Posts: 4
noxious is on a distinguished road

Re: Using for_each in List


ohhhhhhhhh where to find some examples of accesing this elements and oprating with them i think this is done by use of iterators but i don't know to wrtite that to a code
  #7  
Old 16-Aug-2008, 01:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Using for_each in List


Look at the for, while, and do-while statements, as well as arrays and indexing into the arrays.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 21:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
need 'pointers' on deaing with segmentation faults aijazbaig1 C Programming Language 7 17-Aug-2007 08:43
C++ class -- Please help vnca_1 C++ Forum 3 14-Jun-2006 13:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:23.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.