473,378 Members | 1,470 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,378 software developers and data experts.

Accept either an iterator or a reverse_iterator

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
6 1431
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin Magnusson | last post by:
Hi I'm traversing a std::list backwards with a reverse_iterator, and need to check for the case when the iterator points to the first element of the list. My code now looks something like the...
8
by: Alexander Stippler | last post by:
Hello, the standard requires a type reverse_iterator. But in the form it is required and implemented for all compilers I know, there is IMO something missing, which is essential for its usage....
5
by: AlesD | last post by:
Hello, could you please help me with following problem: I have reversible container which holds (unsorted) instances of objects with common base class. From time to time I need to search for...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
3
by: ben | last post by:
If I have a reverse_iterator to an element, does the standard library provide a way to find the iterator equivalent or do I have to roll my own?
13
by: jason | last post by:
My question is regaring STL iterator/reverse_iterator. I would like to write a function which does the following int CFoo::calculate(std::vector<int>::iterator itBegin,...
4
by: KL | last post by:
Well, I got through those other hurdles, but now I am stuck with trying to get an iterator to work. I am having a problem with trying to iterate through the STL list container that I set up. I...
3
by: Dalbosco J-F | last post by:
Hi, Sorry if this has already been answered. Given a std::list and a reverse_iterator is there a way to erase the element pointed to by the reverse_iterator via the erase method? Apparently...
3
by: bb | last post by:
Hi, Please could you clarify why 'implicit conversion' does not take place while assigning an iterator to reverse_iterator. However, it happens while initializing/constructing. e.g. typedef...
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.