"barcaroller" <ba*********@music.netwrote in message news:f3**********@aioe.org...
If an STL container is empty, what will its begin() member function return?
Would it be a valid value that an iterator could use, like the end() member
function? In particular, can I assume that begin() == end()?
begin() and end() will be equal. Both will be valid, can be assigned to iterators,
and can be used in loops, even though they do not point to actual elements:
// Not tested, but I'm
#include<iostream>
#include<list>
#include<string>
int main (void)
{
std::list<std::stringBob; // empty list
std::list<std::string>::iterator i; // iterator for list
for ( i=Bob.begin() ; i!=Bob.end() ; ++i ) // Test "i!=Bob.end()" will fail.
{
std::cout << (*i) << std::endl; // This will never be executed.
}
Bob.push_back("Sam");
Bob.push_back("Tom");
for ( i=Bob.begin() ; i!=Bob.end() ; ++i ) // Test "i!=Bob.end()" will succeed.
{
std::cout << (*i) << std::endl; // This will execute twice.
}
return 0;
}
The first loop does nothing. (Nothing to print.)
The second loop will execute twice and will print:
Sam
Tom
--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org