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

How to test if an iterator is at the end of the container

P: n/a
I have problems finding out how to test weather an iterator are at the
end of its container.

I'd like to do something like the following:

std::list<int>::iterator it = intList.begin();
while (it != intList.end()) {
if (!it.has_next())
std::cout << "This is at the last item in the container\n";

std::cout << "Number: " << *it << std::endl;

it++;
}

How would I do this the Best way? Am I looking past something obvious
here?

Dec 27 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I use to write code like the following when facing this kind of tests:

typedef std::vector<intMyVector;
typedef MyVector::const_iterator MyVectorCIt;

MyVector vect;
vect.push_back(10);
vect.push_back(100);
vect.push_back(1000);

MyVectorCIt it = vect.begin();
MyVectorCIt const end = vect.end();

while(it != end) {
int const& data = *it;
++it;
if(it == end) std::cout << "This is the last item: ";
std::cout << data << '\n';
}

Enselic ha escrit:
I have problems finding out how to test weather an iterator are at the
end of its container.

I'd like to do something like the following:

std::list<int>::iterator it = intList.begin();
while (it != intList.end()) {
if (!it.has_next())
std::cout << "This is at the last item in the container\n";

std::cout << "Number: " << *it << std::endl;

it++;
}

How would I do this the Best way? Am I looking past something obvious
here?
Dec 27 '06 #2

P: n/a

Enselic ΓΡΣαΚ:
I have problems finding out how to test weather an iterator are at the
end of its container.

I'd like to do something like the following:

std::list<int>::iterator it = intList.begin();
while (it != intList.end()) {
if (!it.has_next())
std::cout << "This is at the last item in the container\n";

std::cout << "Number: " << *it << std::endl;

it++;
}

How would I do this the Best way? Am I looking past something obvious
here?
if(it == intList.end()-1)

Dec 27 '06 #3

P: n/a

Enselic wrote:
I have problems finding out how to test weather an iterator are at the
end of its container.

I'd like to do something like the following:

std::list<int>::iterator it = intList.begin();
while (it != intList.end()) {
if (!it.has_next())
std::cout << "This is at the last item in the container\n";

std::cout << "Number: " << *it << std::endl;

it++;
}

How would I do this the Best way? Am I looking past something obvious
here?
There is no such thing as has_next() but you can just check if the
"next" item is end().

while ( it != intList.end() )
{
int x = *it++;

std::cout << x;
if ( it != intList.end() )
{
std::cout << ',';
}
}

If your collection is of a class you can use a reference or const
reference for a const_iterator. Thus:

while ( it != fooList.end() )
{
const foo & f = *it++;
//etc
}

Use a non-const reference if you have a non-const iterator and you are
going to modify the objects or call their non-const methods.

Dec 27 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.