#include "stdafx.h"
#include <ctype.h>
#include <stddef.h>
typedef pair<short, short> location;
typedef vector<location> loc;
typedef vector<string> text;
typedef pair<text*, loc*> text_loc;
/* 定义全局变量*/
vector<string> *lines_of_text; /* 将文本存储到该向量中 */
text_loc *text_locations; /* 存储每一个单词的位置向量 */
map<string, loc*, less<string>> * word_map; /* 每一个单词的位置集合 */
string filt_elems("\",.;:!<<)(\\/");
void retrieve_text();
void separate_words();
void filter_text();
void strip_caps();
void suffix_text();
void suffix_s( string& );
void build_word_map();
int main()
{
retrieve_text();
vector<string>::iterator it = (*lines_of_text).begin();
while ( it != (*lines_of_text).end() )
{
cout<< *it <<endl;
++it;
}
separate_words();
vector<string> *words = text_locations->first;
vector<string>::iterator it1 = (*words).begin();
vector<location> * locals = text_locations->second;
vector<location>::iterator it2 = (*locals).begin();
for ( it1, it2; it1 != (*words).end() && it2 != (*locals).end(); ++it1, ++it2 )
{
cout<< *it1 <<" : " << (*it2).first << (*it2).second <<endl;
}
return 0;
}
void retrieve_text()
{
string file_name;
cout << "please input file name: ";
cin >> file_name;
ifstream infile( file_name.c_str(), ios::in );
if ( !infile )
{
cerr << "oops! unable to open file "
<< file_name << " -- bailing out \n ";
exit( -1 );
}
else
{
cout<< "\n";
}
lines_of_text = new vector<string>;
string textline;
while( getline(infile,textline, '\n' ) )
{
lines_of_text->push_back(textline);
}
}
void separate_words()
{
short line_pos;
vector<string>* words = new vector<string>;
vector<location>* locations = new vector<location>;
for ( line_pos = 0; line_pos < lines_of_text->size(); ++line_pos )
{
string textline = (*lines_of_text)[line_pos];
string::size_type pos = 0, pre_pos = 0;
short words_pos = 0;
while ( (pos = textline.find_first_of(' ', pos )) != string::npos )
{
words->push_back( textline.substr(pre_pos, pos - pre_pos ));
locations->push_back( make_pair(line_pos, words_pos ));
words_pos++;
pos++;
pre_pos = pos;
}
words->push_back( textline.substr(pre_pos, pos - pre_pos ));
locations->push_back( make_pair(line_pos, words_pos ));
}
text_locations = new text_loc(words, locations );
}
C++ STL 练习1
最新推荐文章于 2024-05-05 08:15:00 发布