iterate in a string c++ code example

Example 1: how to iterate throguh a string in c++

void print(const std::string &s)
{
    for (std::string::size_type i = 0; i < s.size(); i++) {
        std::cout << s[i] << ' ';
    }
}

Example 2: string iterator in c++

// string::begin/end
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
    std::cout << *it << endl;
  std::cout << '\n';

  return 0;
}

Tags:

Java Example