运行代码!
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <iostream.h>
#include <fstream.h>
#include <stddef.h>
#include <ctype.h>
typedef pair<short,short> location;
typedef vector<location,allocator> loc;
typedef vector<string,allocator> text;
typedef pair<text*,loc*> text_loc;
class textquery {
public:
textquery() { memset( this, 0, sizeof( textquery )); }
static void filter_elements( string felems ) { filt_elems = felems; }
void query_text();
void display_map_text();
void display_text_locations();
void doit() {
retrieve_text();
separate_words();
filter_text();
suffix_text();
strip_caps();
build_word_map();
}
private:
void retrieve_text();
void separate_words();
void filter_text();
void strip_caps();
void suffix_text();
void suffix_s( string& );
void build_word_map();
private:
vector<string,allocator> *lines_of_text;
text_loc *text_locations;
map<string,loc*,less<string>,allocator> *word_map;
static string filt_elems;
};
string textquery::filt_elems( "\",.;:!?)(\\/" );
int main()
{
textquery tq;
tq.doit();
tq.query_text();
tq.display_map_text();
return 0;
}
void
textquery::
retrieve_text()
{
string file_name;
cout << "please enter 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,allocator>;
string textline;
while ( getline( infile, textline, \n ))
lines_of_text->push_back( textline );
}
void
textquery::
separate_words()
{
vector<string,allocator> *words = new vector<string,allocator>;
vector<location,allocator> *locations = new vector<location,allocator>;
for ( short line_pos = 0; line_pos < lines_of_text->size(); line_pos++ )
{
short word_pos = 0;
string textline = (*lines_of_text)[ line_pos ];
string::size_type eol = textline.length();
string::size_type pos = 0, prev_pos = 0;
while (( pos = textline.find_first_of( , pos )) != string::npos )
{
words->push_back( textline.substr( prev_pos, pos - prev_pos ));
locations->push_back( make_pair( line_pos, word_pos ));
word_pos++; pos++; prev_pos = pos;
}
words->push_back( textline.substr( prev_pos, pos - prev_pos ));
locations->push_back( make_pair( line_pos, word_pos ));
}
text_locations = new text_loc( words, locations );
}
void
textquery::
filter_text()
{
if ( filt_elems.empty() )
return;
vector<string,allocator> *words = text_locations->first;
vector<string,allocator>::iterator iter = words->begin();
vector<string,allocator>::iterator iter_end = words->end();
while ( iter != iter_end )
{
string::size_type pos = 0;
while (( pos = (*iter).find_first_of( filt_elems, pos )) != string::npos )
(*iter).erase(pos,1);
iter++;
}
}
推荐阅读
一编译20几个错
你因改把编译的错误写出来,不是把代码拿上来。
一个最大的错误是在开头没有语句"using namespace std;"


讨论区