Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 2nd, 2008, 10:47 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Library function-like implementation

I want to write a function that takes the begin() and end() values returned by an iterator like many STL functions. I am not sure what type the arguments should be. Is it necessary to use templates? For example:

// function definition - what is type?
void myfunc(type a, type b) { ... }

int main() {
    ...
    vector<double>::iterator iter = ...;
    myfunc(iter.begin(), iter.end());
}
I want to be able to pass any type of iterator, such as list<string>::iterator.
titaniumdecoy is offline   Reply With Quote
Old Jul 3rd, 2008, 12:43 AM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Library function implementation

I have done some searching, and it seems that templates are the way to go. So:

template <typename ForwardIter>
void myfunc(ForwardIter begin, ForwardIter end) { ... }

However, I have also found a number of pre-defined iterator types such as forward_iterator, backward_iterator, etc. How can I use these rather than templates?
titaniumdecoy is offline   Reply With Quote
Old Jul 3rd, 2008, 5:21 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: Library function implementation

Assuming you have a standard container, the type of its iterators are of the form std::container<TYPE>::iterator_type (where container = vector, list, etc, TYPE is the type of element, and iterator_type = iterator, const_iterator, etc).

You also need to be aware that begin(), end(), rbegin(), rend(), etc are - depending on type of container - often overloaded: for example has a std::vector<>::begin() that returns an iterator and another that returns a const_iterator (and possibly other types of iterator, I haven't checked).
grumpy is offline   Reply With Quote
Old Jul 3rd, 2008, 12:11 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Library function implementation

How can I ensure that the arguments to the function above are of iterator_type = forward_iterator?
titaniumdecoy is offline   Reply With Quote
Old Jul 4th, 2008, 4:05 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: Library function implementation

Declare/define your function myfunc() with arguments of type std::container<TYPE>::forward_iterator, and do not overload it for other types. If you attempt to call it with arguments of types other than forward_iterator (or, more precisely, of types that are not implicitly convertible to forward_iterator - which is possibly compiler dependent) you will obtain a compilation error.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning a class function as a callback function core8583 C++ 4 Jun 18th, 2008 7:20 PM
Recommended Practice for returning data from function Arla C# 1 Aug 16th, 2005 12:21 PM
change the empty function from the old format to the new format powah Sed and Awk 0 Jun 23rd, 2005 11:10 AM
Sort function vs. sort_heap function + Transform Function Josef_Stalin C++ 2 May 2nd, 2005 11:18 AM
Returning a value from a variable to the main function colt C 3 Apr 28th, 2005 7:56 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC