Connecting Tech Pros Worldwide Forums | Help | Site Map

STL iterators

Chris Slominski
Guest
 
Posts: n/a
#1: Jul 23 '05
I want to pass begin and end iterators to a method. I must not understand
them correctly. In the example below the DoString signature compiles, but
the operations on the passed parameters are not defined.

Thanks,
Chris


#include <iterator>
#include <string>
#include <list>
#include <vector>
using namespace std;

void Tester::DoStrings(iterator<forward_iterator_tag, string> begin,
iterator<forward_iterator_tag, string> end)
{
*begin; // undefined operation
++begin; // undefined operation
}

...

{
list<string> myList;
DoStrings(myList.begin(), myList.end());

vector<string> myVector;
DoString(myVector.begin(), myVector.end());
}



red floyd
Guest
 
Posts: n/a
#2: Jul 23 '05

re: STL iterators


Chris Slominski wrote:[color=blue]
> I want to pass begin and end iterators to a method. I must not understand
> them correctly. In the example below the DoString signature compiles, but
> the operations on the passed parameters are not defined.
>
> Thanks,
> Chris
>
>
> #include <iterator>
> #include <string>
> #include <list>
> #include <vector>
> using namespace std;
>
> void Tester::DoStrings(iterator<forward_iterator_tag, string> begin,
> iterator<forward_iterator_tag, string> end)
> {
> *begin; // undefined operation
> ++begin; // undefined operation
> }
>
> ...
>
> {
> list<string> myList;
> DoStrings(myList.begin(), myList.end());
>
> vector<string> myVector;
> DoString(myVector.begin(), myVector.end());
> }
>
>
>[/color]

try:

template <typename ForwardIterator>
void Tester::DoStrings(ForwardIterator begin, ForwardIterator end)
{
*begin;
++begin;
}
Dietmar Kuehl
Guest
 
Posts: n/a
#3: Jul 23 '05

re: STL iterators


Chris Slominski wrote:[color=blue]
> I must not understand them correctly.[/color]

I'd consider this to be a gross understatement...
[color=blue]
> void Tester::DoStrings(iterator<forward_iterator_tag, string> begin,
> iterator<forward_iterator_tag, string> end)[/color]

There is no such thing like an iterator base class, at least none
which has any functionality. 'std::iterator<>' essentially just adds
necessary declaration of associated types (e.g. the value type, an
iterator category, etc.) but has no functionality on its own. You
probably want to accept arbitrary forward iterators whose value type
is 'std::string'. The easiest approach to do so is to declare your
function like this:

template <typename ForwardIt>
void Test::DoStrings(ForwardIt begin, ForwardIt end)[color=blue]
> {
> *begin; // undefined operation
> ++begin; // undefined operation
> }[/color]
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Chris Slominski
Guest
 
Posts: n/a
#4: Jul 23 '05

re: STL iterators



"red floyd" <no.spam@here.dude> wrote in message
news:WEnTd.8980$Pz7.2837@newssvr13.news.prodigy.co m...[color=blue]
> Chris Slominski wrote:[color=green]
> > I want to pass begin and end iterators to a method. I must not[/color][/color]
understand[color=blue][color=green]
> > them correctly. In the example below the DoString signature compiles,[/color][/color]
but[color=blue][color=green]
> > the operations on the passed parameters are not defined.
> >
> > Thanks,
> > Chris
> >
> >
> > #include <iterator>
> > #include <string>
> > #include <list>
> > #include <vector>
> > using namespace std;
> >
> > void Tester::DoStrings(iterator<forward_iterator_tag, string> begin,
> > iterator<forward_iterator_tag, string> end)
> > {
> > *begin; // undefined operation
> > ++begin; // undefined operation
> > }
> >
> > ...
> >
> > {
> > list<string> myList;
> > DoStrings(myList.begin(), myList.end());
> >
> > vector<string> myVector;
> > DoString(myVector.begin(), myVector.end());
> > }
> >
> >
> >[/color]
>
> try:
>
> template <typename ForwardIterator>
> void Tester::DoStrings(ForwardIterator begin, ForwardIterator end)
> {
> *begin;
> ++begin;
> }[/color]

Thanks for the help and I will likely use the general solution you offer.
Just out of curiosity though, vector<string>::iterator,
list<string>::iterator, set<string>::iterator are all typedef 'ed as some
concrete type. Are the three concrete types related by some common ancestor
that supports the auto-increment and dereference operator?

Chris


Clark S. Cox III
Guest
 
Posts: n/a
#5: Jul 23 '05

re: STL iterators


On 2005-02-24 12:51:55 -0500, "Chris Slominski" <cjs@jlab.org> said:
[color=blue]
>
> "red floyd" <no.spam@here.dude> wrote in message
> news:WEnTd.8980$Pz7.2837@newssvr13.news.prodigy.co m...[color=green]
>>
>> template <typename ForwardIterator>
>> void Tester::DoStrings(ForwardIterator begin, ForwardIterator end)
>> {
>> *begin;
>> ++begin;
>> }[/color]
>
> Thanks for the help and I will likely use the general solution you offer.
> Just out of curiosity though, vector<string>::iterator,
> list<string>::iterator, set<string>::iterator are all typedef 'ed as some
> concrete type. Are the three concrete types related by some common ancestor
> that supports the auto-increment and dereference operator?
>[/color]

No. I suppose that they could be, on some particular platform, but you
would gain little in doing so, as standard-compliant code could never
make any use of that knowledge anyway.


--
Clark S. Cox, III
clarkcox3@gmail.com

Closed Thread