473,503 Members | 12,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic iterator utility functions

Let's say I want to write the following function,

void f(const list<int> &l, const vector<int> &v) {
cout << get_second(l) << '\n';
cout << get_second(v) << '\n';
cout << get_nth(l, 2) << '\n';
cout << get_nth(v, 2) << '\n';
}

that prints the second and third (counting from zero) items of given
list and vector.
For such a purpose I need to write the generic functions (i.e. function
templates) 'get_second' and'get_nth' that, respectively, return the
second and the n-th item of the given collection. Possible
implementations of these two functions are the following ones:

template<typename Coll>
inline typename Coll::value_type get_second(const Coll &coll) {
// return *++(coll.begin()); // Wrong.
// return *(coll.begin() + 1); // Wrong.
typename Coll::const_iterator it = coll.begin();
advance(it, 1);
return *it;
}

template<typename Coll, typename Distance>
inline typename Coll::value_type get_nth(
const Coll &coll, Distance d) {
typename Coll::const_iterator it = coll.begin();
advance(it, d);
return *it;
}

As noted in the code comments, the use of the operator '++' is wrong
(although some compilers accept it) as the function 'begin' shouldn't
return an l-value; and the use of the operator'+' is wrong for
non-random iterators, like those of list. So, the quickest solution I
found was that rather cumbersome code.
But I think that a better solution would be to define the following
function templates:

// Return the successor of the given input iterator.
template<typename InputIterator>
inline InputIterator succ(InputIterator it) {
return ++it;
}

// Return the predecessor of the given bidirectional iterator.
template<typename BidirectionalIterator>
inline BidirectionalIterator pred(BidirectionalIterator it) {
return --it;
}

// Return an input iterator moved from the given input iterator
// by the given distance.
template<typename InputIterator, typename Distance>
inline InputIterator adv(InputIterator it, Distance d) {
advance(it, d);
return it;
}

and then use them in the following function templates:

template<typename Coll>
inline typename Coll::value_type get_second(const Coll &coll) {
return *succ(coll.begin());
}

template<typename Coll, typename Distance>
inline typename Coll::value_type get_nth(const Coll &coll,
Distance d) {
return *adv(coll.begin(), d);
}

Is it a good idea to keep the functions 'succ', 'prec' and 'adv' in my
toolkit of generic utility functions, or are there better solutions?

--
Carlo Milanesi
http://digilander.libero.it/carlmila
Jul 19 '05 #1
0 2333

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

Similar topics

26
1514
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
13
5056
by: Mike Austin | last post by:
Hi all. Just working on a small virtual machine, and thought about using vector iterators instead of pointer arithmetic. Question is, why does an iterator plus any number out of range not...
4
4408
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
4
1585
by: Arturo Cuebas | last post by:
I've got a bunch of file_iterator<> (http://tinyurl.com/3uuxa) begin/end pairs that point to various chunks in a file. It would be super-cool, in my program, to be able to treat all of these ranges...
2
1996
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the...
21
5679
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
6
1913
by: rep_movsd | last post by:
Hi folks I was on topcoder and came across an interesting problem... It involved dynamic programming ( storing function results in a map to avoid repeated computation ) and I ended up having...
5
11107
by: Torben Laursen | last post by:
I am writing a COM in C# using visual studio 2005 and VSTO. Inside the code I use some support classes that are generic but they are not used in the inferface of the COM. However I still get a...
6
1688
by: StephQ | last post by:
I need to implement an algorithm that takes as input a container and write some output in another container. The containers involved are usually vectors, but I would like not to rule out the...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.