473,383 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Passing forward iterator as parameter

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.

Jan 11 '07 #1
4 9181
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
Jan 11 '07 #2


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.
Jan 11 '07 #3
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++.
Jan 11 '07 #4

Michael Domenic DiBernardo wrote:
<snip>

why not use boost pointer containers instead ?

Jan 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
5
by: Thomas Matthews | last post by:
Hi, I have a Display class. I would like to write a function that takes a range of objects and displays them. The range would be specified by two forward iterators: start and end (one past...
3
by: CoolPint | last post by:
I have implemented a generic priority queue below and tested it works fine, but I have one small problem I cannot understand. I have type parameter F which determines the priority so that users can...
8
by: Jim Langston | last post by:
I have a class I designed that stores text chat in a std::vector<sd::string>. This class has a few methods to retrieve these strings to be displayed on the screen. void ResetRead( bool Reverse,...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
3
by: sd2004 | last post by:
I am still learning, could someone show/explain to me how to fix the error. I can see it is being wrong but do not know how to fix. could you also recommend a book that I can ref. to ?...
5
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want...
1
by: flopbucket | last post by:
Hi, For the learning experience, I am building a replacement for std::map. I built a templated red-black tree, etc., and all the basic stuff is working well. I implemented basic iterators and...
1
by: Lambda | last post by:
Hi, All, Why the search() function use Forward Iterator? I think Input Iterator is enough, I need read only. In such kind of situations, how should I choose iterator type?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.