STL iterators

C

Chris Slominski

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());
}
 
R

red floyd

Chris said:
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());
}

try:

template <typename ForwardIterator>
void Tester::DoStrings(ForwardIterator begin, ForwardIterator end)
{
*begin;
++begin;
}
 
D

Dietmar Kuehl

Chris said:
I must not understand them correctly.

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

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:
 
C

Chris Slominski

red floyd said:
try:

template <typename ForwardIterator>
void Tester::DoStrings(ForwardIterator begin, ForwardIterator end)
{
*begin;
++begin;
}

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
 
C

Clark S. Cox III

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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top