Here is another example.
Code:
#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::cout;
using std::iter;
using std::string;
vector<string> Split( const string&);
void PrintVec ( const vector<string> &);
int main()
{
vector<string> Token;
string address;
cout << "Enter an address: ";
getline(cin, address);
cout << "The address you entered is: " << address << endl;
Token = Split(address);
PrintVec(Token);
return 0;
}
vector<string> Split (const string & s)
{
vector<string> ret;
string::size_type i = 0;
while ( i != s.size() )
{
//skip leading blanks
while ( i != s.size() && isspace(s[i]) ) ++i;
//find end of next word
string::size_type j = i;
while ( j != s.size() && !isspace(s[j]) ) ++j;
//if we found some whitespace characters
if ( i != j)
{
//copy from s starting at i and taking j - i characters
ret.push_back( s.substr( i , j - i) );
i = j;
}
}
return ret;
}
void PrintVec (const vector<string> & Token)
{
vector<string>::const_iterator i = Token.begin();
while ( i != Token.end() )
{
cout << "Token: " << *i << endl;
i++;
}
}