Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing forward iterator as parameter

Reply
Thread Tools

Passing forward iterator as parameter

 
 
Michael Domenic DiBernardo
Guest
Posts: n/a
 
      01-11-2007
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-11-2007
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


 
Reply With Quote
 
 
 
 
Michael Domenic DiBernardo
Guest
Posts: n/a
 
      01-11-2007


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.
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      01-11-2007
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++.
 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      01-11-2007

Michael Domenic DiBernardo wrote:
<snip>

why not use boost pointer containers instead ?

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
Forward iterators and past-the-end iterator Mark Stijnman C++ 5 01-28-2006 08:56 PM
Re-forward declaration of types which were already forward declared qazmlp C++ 1 02-15-2004 07:00 PM
A Forward Iterator type / class? Thomas Matthews C++ 5 12-19-2003 06:52 PM
Example of a user-defined forward iterator? Brett L. Moore C++ 3 07-15-2003 03:38 AM



Advertisments