Passing forward iterator as parameter

Discussion in 'C++' started by Michael Domenic DiBernardo, Jan 11, 2007.

  1. I posted this on comp.std.c++ because it seemed a more appropriate
    question for that forum, but I'm more or less in a hurry and so I decided
    to toss it out here as well:

    How might I declare a function that takes a forward_iterator as a
    parameter? I want a generic function that will iterate over a given
    forward iterator and free all of the pointers contained therein.

    The gist:

    #include <iterator>

    void deleteAll(std::forward_iterator begin, std::forward_iterator end) {
    while (begin != end) {
    delete *begin;
    ++begin;
    }
    }

    Silly usage scenario (assuming existence of class MyObj):

    vector<MyObj*> v;
    v.push_back(new MyObj());
    v.push_back(new MyObj());
    v.push_back(new MyObj());

    deleteAll(v.begin(), v.end());

    Any tips or references would be greatly appreciated!

    Thanks,
    -M.D.
     
    Michael Domenic DiBernardo, Jan 11, 2007
    #1
    1. Advertising

  2. Michael Domenic DiBernardo wrote:
    > I posted this on comp.std.c++ because it seemed a more appropriate
    > question for that forum, but I'm more or less in a hurry and so I
    > decided to toss it out here as well:
    >
    > How might I declare a function that takes a forward_iterator as a
    > parameter? I want a generic function that will iterate over a given
    > forward iterator and free all of the pointers contained therein.


    How would it know when to stop?

    >
    > The gist:
    >
    > #include <iterator>
    >
    > void deleteAll(std::forward_iterator begin, std::forward_iterator


    Ah, so *two* iterators, then.

    > end) { while (begin != end) {
    > delete *begin;
    > ++begin;


    Or just

    delete *begin++;

    > }
    > }
    >
    > Silly usage scenario (assuming existence of class MyObj):
    >
    > vector<MyObj*> v;
    > v.push_back(new MyObj());
    > v.push_back(new MyObj());
    > v.push_back(new MyObj());
    >
    > deleteAll(v.begin(), v.end());
    >
    > Any tips or references would be greatly appreciated!


    You _have_ to make it a template:

    template<class ForwardIter>
    void deleteAll(ForwardIter begin, ForwardIter end) {
    ... // same body


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 11, 2007
    #2
    1. Advertising

  3. On Wed, 10 Jan 2007, Victor Bazarov wrote:
    > You _have_ to make it a template:
    >
    > template<class ForwardIter>
    > void deleteAll(ForwardIter begin, ForwardIter end) {
    > ... // same body


    Guh, I totally should have figured that out on my own. Thanks so much!

    This is what happens when I return to C++ after programming in OCaml for 6
    months...

    -M.D.
     
    Michael Domenic DiBernardo, Jan 11, 2007
    #3
  4. On Wed, 10 Jan 2007 22:04:17 -0500, "Victor Bazarov" wrote:
    >Michael Domenic DiBernardo wrote:
    >> How might I declare a function that takes a forward_iterator as a
    >> parameter? I want a generic function that will iterate over a given
    >> forward iterator and free all of the pointers contained therein.

    >
    >You _have_ to make it a template:
    >
    > template<class ForwardIter>
    > void deleteAll(ForwardIter begin, ForwardIter end) {
    > ... // same body


    This is an example of the 'templates create more templates' pinciple
    in C++.
     
    Roland Pibinger, Jan 11, 2007
    #4
  5. Michael Domenic DiBernardo

    dasjotre Guest

    Michael Domenic DiBernardo wrote:
    <snip>

    why not use boost pointer containers instead ?
     
    dasjotre, Jan 11, 2007
    #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. Brett L. Moore
    Replies:
    3
    Views:
    1,970
    Glen Low
    Jul 15, 2003
  2. Thomas Matthews

    A Forward Iterator type / class?

    Thomas Matthews, Dec 18, 2003, in forum: C++
    Replies:
    5
    Views:
    471
    Jeffrey Schwab
    Dec 19, 2003
  3. qazmlp
    Replies:
    1
    Views:
    615
    Jonathan Turkanis
    Feb 15, 2004
  4. Mark Stijnman
    Replies:
    5
    Views:
    515
    Mark Stijnman
    Jan 28, 2006
  5. Lambda
    Replies:
    2
    Views:
    335
Loading...

Share This Page