Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created December 26, 2010 18:46
Show Gist options
  • Save johnhmj/755559 to your computer and use it in GitHub Desktop.
Save johnhmj/755559 to your computer and use it in GitHub Desktop.
// @eyny@Force[TW]@
#include "forcetw20101226b001.h"
//
CStrTok::CStrTok(void)
{
this->m_str.clear();
this->m_token = NULL;
this->m_vstr.clear();
}
CStrTok::CStrTok(const string str)
{
if ( str.empty() )
{
return;
}
stringstream ss(str);
ss >> this->m_str;
ss >> this->m_token;
this->m_vstr.clear();
this->Token();
}
CStrTok::~CStrTok(void)
{
this->m_str.clear();
this->m_token = NULL;
this->m_vstr.clear();
}
void CStrTok::setString(const string str)
{
if ( str.empty() )
{
return;
}
stringstream ss(str);
ss >> this->m_str;
ss >> this->m_token;
this->m_vstr.clear();
this->Token();
}
void CStrTok::Token(void)
{
if ( this->m_str.empty() )
{
return;
}
string buffer = this->m_str;
unsigned int i = 0;
for (; i < buffer.size(); i ++)
{
if ( buffer[i] == this->m_token )
{
buffer[i] = ' ';
}
}
stringstream ss(buffer);
buffer.clear();
while ( ss >> buffer )
{
this->m_vstr.push_back(buffer);
}
}
void CStrTok::Print(void)
{
if ( this->m_vstr.empty() )
{
return;
}
for (vStr::iterator it = this->m_vstr.begin(); it != this->m_vstr.end(); it ++)
{
cout << (*it) << endl;
}
}
// @eyny@Force[TW]@
#ifndef _FORCETW20101226B001_H_
#define _FORCETW20101226B001_H_
//
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
//
typedef vector<string> vStr;
//
class CStrTok
{
public:
string m_str;
char m_token;
vStr m_vstr;
CStrTok(void);
CStrTok(const string str);
~CStrTok(void);
void setString(const string str);
void Token(void);
void Print(void);
};
//
#endif
// @eyny@Force[TW]@
// Integrated Development Environment
// Visual C++
#include <iostream>
#include <string>
#include "forcetw20101226b001.h"
using namespace std;
//
int main(int argc, char** argv)
{
CStrTok* st = new CStrTok();
string str;
while ( getline(cin, str) )
{
st->setString(str);
st->Print();
cout << endl;
str.clear();
}
delete st;
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment