Strings: size, iterator, count, begin and end : String « Data Type « C++






Strings: size, iterator, count, begin and end

Strings: size, iterator, count, begin and end
 
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
  string str1("www.java2s.com");
  string::iterator p;
  unsigned int i;

  // use size()
  for(i = 0; i <str1.size(); i++)
    cout << str1[i];
  cout << endl;

  // use iterator
  p = str1.begin();
  while(p != str1.end()) 
    cout << *p++;
  cout << endl;

  // use the count() algorithm
  i = count(str1.begin(), str1.end(), 'i');
  cout << "There are " << i << " i's in str1\n";

  p = str1.begin();
  while(p != str1.end()) 
    cout << *p++;
  cout << endl;


  return 0;
}


           
         
  








Related examples in the same category

1.String type classString type class
2.A filter to remove white-space characters at the ends of lines.A filter to remove white-space characters at the ends of lines.
3.A string demonstration: assignment, concatenate, compareA string demonstration: assignment, concatenate, compare
4.Demonstrate insert(), erase(), and replace().Demonstrate insert(), erase(), and replace().
5.Use string: find, string::nposUse string: find, string::npos
6.string: find( ) and rfind( )string: find( ) and rfind( )
7.Print a name in two different formatsPrint a name in two different formats
8.Several string operations: substrSeveral string operations: substr
9.String Char IndexingString Char Indexing
10.String Find and replaceString Find and replace
11.String SizeString Size
12.String SizeOfString SizeOf
13.A short string demonstrationA short string demonstration
14.String insert(), erase(), and replace()String insert(), erase(), and replace()
15.string variable instead of a character arraystring variable instead of a character array
16.Inputting Multiple Words into a StringInputting Multiple Words into a String
17.Adding StringsAdding Strings
18.Read string from consoleRead string from console
19.Insert, search, and replace in strings.Insert, search, and replace in strings.
20.Accessing Characters In StringsAccessing Characters In Strings