The following program computes the length of the string using the 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
counter++;
cout<<" "<<*it;
}
cout<<"\n"<<"Total length of the string : "<<counter;
return 0;
} // end of main
------OUTPUT------
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.
Great job DUDe
ReplyDeleteThanx :) Please feel free to browse through my other posts.
DeleteIt is a pointer or variable ?
ReplyDeletenotice its behaviour by removing *before it in cout statement
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.
DeleteThe 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.
This is a good article & good site.Thank you for sharing this article. It is help us following categorize:
ReplyDeleteit 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,
prologic-corp