list edit

Discussion in 'C++' started by swtsvn, Sep 11, 2008.

  1. swtsvn

    swtsvn Guest

    hi all
    iam trying to use list from stl
    and i did not find any method to update the value of a list item
    for example
    listvalue++ for each node in the list
    could any of u tell me how to update a value in the list?
    swtsvn, Sep 11, 2008
    #1
    1. Advertising

  2. swtsvn

    Rolf Magnus Guest

    swtsvn wrote:

    > hi all
    > iam trying to use list from stl
    > and i did not find any method to update the value of a list item
    > for example
    > listvalue++ for each node in the list
    > could any of u tell me how to update a value in the list?


    Use an iterator and dereference it. For the specific case of incrementing
    each element, you can use for_each:

    // #include <algorithm> and <functional> before this
    // replace value_type with the elemnt type of your list.
    for_each(mylist.begin(), mylist.end(), bind2nd(plus<value_type>(), 1));

    Anything other than such trivial examples is best done using an ordinary for
    loop iterating from begin() to end().
    Rolf Magnus, Sep 11, 2008
    #2
    1. Advertising

  3. swtsvn

    Guest

    On 11 Sep, 05:14, Rolf Magnus <> wrote:
    > swtsvn wrote:
    > > hi all
    > > iam trying to use list from stl
    > > and i did not find any method to update the value of a list item
    > > for example
    > > listvalue++ for each node in the list
    > > could any of u tell me how to update a value in the list?

    >
    > Use an iterator and dereference it. For the specific case of incrementing
    > each element, you can use for_each:
    >
    > // #include <algorithm> and <functional> before this
    > // replace value_type with the elemnt type of your list.
    > for_each(mylist.begin(), mylist.end(), bind2nd(plus<value_type>(), 1));
    >
    > Anything other than such trivial examples is best done using an ordinary for
    > loop iterating from begin() to end().


    for_each doesn't quite do it - think it will throw the return value
    away. transform will update the container, so this works and outputs
    "2,3,4,"

    #include <algorithm>
    #include <functional>
    #include <iostream>
    #include <list>

    using namespace std; // For brevity in this e-mail

    int main()
    {
    std::list<int> li;
    li.push_back(1);
    li.push_back(2);
    li.push_back(3);

    transform(li.begin(),li.end(),li.begin(),bind2nd(plus<int>(),1));
    copy(li.begin(),li.end(),ostream_iterator<int>(std::cout,","));
    }
    , Sep 11, 2008
    #3
  4. swtsvn

    Rolf Magnus Guest

    wrote:

    > On 11 Sep, 05:14, Rolf Magnus <> wrote:
    >> swtsvn wrote:
    >> > hi all
    >> > iam trying to use list from stl
    >> > and i did not find any method to update the value of a list item
    >> > for example
    >> > listvalue++ for each node in the list
    >> > could any of u tell me how to update a value in the list?

    >>
    >> Use an iterator and dereference it. For the specific case of incrementing
    >> each element, you can use for_each:
    >>
    >> // #include <algorithm> and <functional> before this
    >> // replace value_type with the elemnt type of your list.
    >> for_each(mylist.begin(), mylist.end(), bind2nd(plus<value_type>(), 1));
    >>
    >> Anything other than such trivial examples is best done using an ordinary
    >> for loop iterating from begin() to end().

    >
    > for_each doesn't quite do it - think it will throw the return value
    > away. transform will update the container, so this works and outputs
    > "2,3,4,"


    Yes, you're right. I shouldn't post in the moring when I'm in a hurry.
    Rolf Magnus, Sep 11, 2008
    #4
  5. swtsvn

    swtsvn Guest

    thanks to both, :)
    will try transform and copy and let u know how it comes about



    On Sep 11, 11:12 am, Rolf Magnus <> wrote:
    > wrote:
    > > On 11 Sep, 05:14, Rolf Magnus <> wrote:
    > >> swtsvn wrote:
    > >> > hi all
    > >> > iam trying to use list from stl
    > >> > and i did not find any method to update the value of a list item
    > >> > for example
    > >> > listvalue++ for each node in the list
    > >> > could any of u tell me how to update a value in the list?

    >
    > >> Use an iterator and dereference it. For the specific case of incrementing
    > >> each element, you can use for_each:

    >
    > >> // #include <algorithm> and <functional> before this
    > >> // replace value_type with the elemnt type of your list.
    > >> for_each(mylist.begin(), mylist.end(), bind2nd(plus<value_type>(), 1));

    >
    > >> Anything other than such trivial examples is best done using an ordinary
    > >> for loop iterating from begin() to end().

    >
    > > for_each doesn't quite do it - think it will throw the return value
    > > away. transform will update the container, so this works and outputs
    > > "2,3,4,"

    >
    > Yes, you're right. I shouldn't post in the moring when I'm in a hurry.
    swtsvn, Sep 15, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. San Diego Guy
    Replies:
    0
    Views:
    531
    San Diego Guy
    Aug 7, 2003
  2. Schultz
    Replies:
    3
    Views:
    544
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 14, 2005
  3. =?Utf-8?B?a2Vu?=
    Replies:
    1
    Views:
    10,309
    Wiktor Zychla [C# MVP]
    Jan 23, 2006
  4. Sharon McCarty
    Replies:
    0
    Views:
    240
    Sharon McCarty
    Nov 24, 2004
  5. Frank Kurka
    Replies:
    8
    Views:
    288
Loading...

Share This Page