By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,170 Members | 1,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,170 IT Pros & Developers. It's quick & easy.

Accept either an iterator or a reverse_iterator

P: n/a
Hi,
I'm writng a function that works an a std::string and should accept either an
iterator or a reverse_iterator. How can I do this? O:-)
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Boogie El Aceitoso" <fr****@telefonica.net> wrote in message
news:pj********************************@4ax.com...
Hi,
I'm writng a function that works an a std::string and should accept either an iterator or a reverse_iterator. How can I do this? O:-)


Function parameter overloading?

Regards,
Elias
Jul 22 '05 #2

P: n/a
On Wed, 03 Dec 2003 16:17:52 +0100, Boogie El Aceitoso wrote:
Hi,
I'm writng a function that works an a std::string and should accept either an
iterator or a reverse_iterator. How can I do this? O:-)


The way this is done most often, is to use a template:

template<typename T1, typename T2>
void print(T1 start, T2 end)
{
while (start != end)
cout << *start++;
}

HTH,
M4

Jul 22 '05 #3

P: n/a
On Wed, 03 Dec 2003 16:52:10 +0100, Martijn Lievaart wrote:
On Wed, 03 Dec 2003 16:17:52 +0100, Boogie El Aceitoso wrote:
Hi,
I'm writng a function that works an a std::string and should accept either an
iterator or a reverse_iterator. How can I do this? O:-)
The way this is done most often, is to use a template:

template<typename T1, typename T2>
void print(T1 start, T2 end)


Shouldn't the iterators be the same type?

template<typename Iter>
void print(Iter start, Iter end)
{
while (start != end)
cout << *start++;
}

HTH,
M4


Josh
Jul 22 '05 #4

P: n/a
On Wed, 03 Dec 2003 17:27:29 -0500, Josh Sebastian wrote:

Shouldn't the iterators be the same type?

template<typename Iter>
void print(Iter start, Iter end)
{
while (start != end)
cout << *start++;
}


No, this precludes the use of a const_iterator for one and a normal
iterator for the other. For most cases it will work though.

M4

Jul 22 '05 #5

P: n/a
On Thu, 04 Dec 2003 09:25:37 +0100, Martijn Lievaart wrote:
On Wed, 03 Dec 2003 17:27:29 -0500, Josh Sebastian wrote:

Shouldn't the iterators be the same type?

template<typename Iter>
void print(Iter start, Iter end)
{
while (start != end)
cout << *start++;
}

No, this precludes the use of a const_iterator for one and a normal
iterator for the other.


Maybe I'm just lacking in imagination, but that doesn't seem terribly
useful to me. In theory, the two iterators should point to elements of the
same sequence. I don't really see why you'd have a const- and
non-const-iterator to the same sequence.
For most cases it will work though.


I'd hope so, as that's how the standard library functions do it. :-}

Josh
Jul 22 '05 #6

P: n/a
On Thu, 04 Dec 2003 17:47:21 -0500, Josh Sebastian wrote:
On Thu, 04 Dec 2003 09:25:37 +0100, Martijn Lievaart wrote:
On Wed, 03 Dec 2003 17:27:29 -0500, Josh Sebastian wrote:

Shouldn't the iterators be the same type?


No, this precludes the use of a const_iterator for one and a normal
iterator for the other.


Maybe I'm just lacking in imagination, but that doesn't seem terribly
useful to me. In theory, the two iterators should point to elements of the
same sequence. I don't really see why you'd have a const- and
non-const-iterator to the same sequence.
For most cases it will work though.


I'd hope so, as that's how the standard library functions do it. :-}


Hmm, I did it that way, because I remembered that the STL does it my way.
I was wrong. It is only relevant when the iterators really differ, like
input/output iterators.

Thanks for the correction.

M4

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.