Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to test if an iterator is at the end of the container

Reply
Thread Tools

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

 
 
Enselic
Guest
Posts: n/a
 
      12-27-2006
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?

 
Reply With Quote
 
 
 
 
Berny Cantos
Guest
Posts: n/a
 
      12-27-2006
I use to write code like the following when facing this kind of tests:

typedef std::vector<int> MyVector;
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?


 
Reply With Quote
 
 
 
 
mohammad.nabil.h@gmail.com
Guest
Posts: n/a
 
      12-27-2006

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)

 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      12-27-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57