Copyright © Programs ++
Design by Dzignine
Tuesday, 22 May 2012

Use of string iterator in C++

The following program computes the length of the string using the string iterator

// Program to demonstrate the use of string iterator
#include <iostream>
#include <string>

int main()
     using namespace std;
     int counter=0;
     string str("welcome to programsplusplus");
     // declares the string iterator
     string::iterator it;
     for (it=str.begin(); it<str.end(); it++)
          // displays each character that the iterator points to
          cout<<" "<<*it;
     cout<<"\n"<<"Total length of the string : "<<counter;

     return 0;
} // end of main

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.


  1. Replies
    1. Thanx :) Please feel free to browse through my other posts.

  2. It is a pointer or variable ?

    notice its behaviour by removing *before it in cout statement

    1. yes it is actually a pointer variable of string type. Iterators are actually variables that are used to traverse containers. Almost all containers have an iterator associated with it.

      The string iterator can be used to traverse the individual characters of the string. You can also use it with the ++ and -- (poth pre and post) operators as well.

      Yes i have seen the behavior when the * is removed.
      Tip: try removing the * and replace it with the &. You will get a clearer view as to how the iterator works.

  3. This is a good article & good site.Thank you for sharing this article. It is help us following categorize:
    it consulting, retail, manufacturing, CRM, digital supply chain management, Delivering high-quality service for your business applications,
    Solutions for all Industries,
    Getting your applications talking is the key to better business processes,
    Rapid web services solutions for real business problems,
    Understanding Your Data is the Key to Good Decision-Making,
    Web-based Corporate Document Management System,
    Outsourcing Solution,
    Financial and Operations Business Intelligence Solution,



Comment Here....