`
datoplay
  • 浏览: 1614642 次
文章分类
社区版块
存档分类
最新评论

My exercise yesterday: Design by Using C++

 
阅读更多

Yesterday, I wrote the code as below to seperate every word in a file,and then display. I wrote the sentence more C++ functions,and feel good:)

#include "iostream"
#include "fstream"
#include "vector"
#include "algorithm"

using namespace std;
vector<char> vec_ch;
vector< vector<char> > vecStr;
void process(char* ch)
{
bool bWordBegin = false;
while(*ch)
{
if(*ch != '/x20' && *ch != '/x0D' && *ch != '/x0A' && *ch != '/x09') //not delimerator
{
bWordBegin = true;
vec_ch.push_back(*ch);
}
else
{
if(bWordBegin)
{
vecStr.push_back(vec_ch);
//vec_ch.empty();
vec_ch.clear();
bWordBegin = false;
}
}

ch++;
}
if(bWordBegin)
{
vecStr.push_back(vec_ch);
vec_ch.clear();
}
}
void Output_char(char ch)
{
cout << ch;
}

void Output( vector<char> v1)
{
//for_each(v1.begin(), v1.end(), Output_char);
copy(v1.begin(), v1.end(), ostream_iterator<char>(cout));
cout << endl;

}
void main(void)
{
char ch[500];
ch[0] = '/r';
ch[1] = '/n';
ifstream in("test.txt");
while(in.getline(ch, 100))
{
process(ch);
//cout << ch <<endl;
}
vector< vector<char> >::iterator iterStr;
//typedef void (* fp)(void) pfp;
//pfp pfp1;
//pfp1 = Output;
for_each(vecStr.begin(), vecStr.end(), Output); //pfp1);


}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics