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

STL iterators

P: n/a
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());
}

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Chris Slominski wrote:
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;
}
Jul 23 '05 #2

P: n/a
Chris Slominski wrote:
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:

template <typename ForwardIt>
void Test::DoStrings(ForwardIt begin, ForwardIt end) {
*begin; // undefined operation
++begin; // undefined operation
}

--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #3

P: n/a

"red floyd" <no*****@here.dude> wrote in message
news:WE*****************@newssvr13.news.prodigy.co m...
Chris Slominski wrote:
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;
}


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
Jul 23 '05 #4

P: n/a
On 2005-02-24 12:51:55 -0500, "Chris Slominski" <cj*@jlab.org> said:

"red floyd" <no*****@here.dude> wrote in message
news:WE*****************@newssvr13.news.prodigy.co m...

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?


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
cl*******@gmail.com

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.