Using an iterator to output a string : string iterator « String « C++






Using an iterator to output a string

  
 
#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

int main()
{
   string string1( "Testing iterators" );
   string::const_iterator iterator1 = string1.begin();

   cout << "string1 = " << string1
      << "\n(Using iterator iterator1) string1 is: ";

   while ( iterator1 != string1.end() ) 
   {
      cout << *iterator1; 
      iterator1++;        
   }

   cout << endl;
   return 0;
}

/* 
string1 = Testing iterators
(Using iterator iterator1) string1 is: Testing iterators

 */        
    
  








Related examples in the same category

1.use iterator
2.string::npos
3.Create an iterator to a string and Use it to cycle through the characters of a string
4.increment the iterator
5.Access the contents of a string using iterators