![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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());
} |
|
|
|
|
|
#2 |
|
Expert Programmer
|
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? |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
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). |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Re: Library function implementation
How can I ensure that the arguments to the function above are of iterator_type = forward_iterator?
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |