Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

begin() for empty STL container


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()?

On May 30, 10:28 pm, "barcaroller" <barcarol@music.net> wrote:

> 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()?

Yes, but that doesn't make it valid, the container is empty.

"barcaroller" <barcarol@music.net> wrote in message news:f3lbsk$m6g$1@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::string> Bob;                 // 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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc