谁在学C++Primer(3),第六章的完整程序,在C++Builder中编译通过,在VC60中编译通不过? 有十几个警告,还有一个说是执行CL.exe 错误。???? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #include <algorithm>#include <string>#include <vector>#include <utility>#include <map>#include <set>#include <fstream>#include <iostream>#include <stddef.h>#include <ctype.h>using namespace std;typedef pair <short,short> location;typedef vector <location> loc;typedef vector <string> text1;typedef pair<text1 *,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> *lines_of_text; text_loc *text_locations; map<string,loc* > *word_map; static string filt_elems;};string TextQuery::filt_elems("\",.;:!?)(\\/" );int main(){ TextQuery tq; tq.doit(); tq.query_text(); tq.display_map_text();}void TextQuery::retrieve_text(){ std::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!"; 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 TextQuery::separate_words(){ vector<string> *words=new vector<string>; vector<location> *locations=new vector<location>; 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> *words=text_locations->first; vector<string>::iterator iter=words->begin(); vector<string>::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; }}void TextQuery::suffix_text(){ vector<string> *words=text_locations->first; vector<string>::iterator iter=words->begin(); vector<string>::iterator iter_end=words->end(); while(iter!=iter_end) { if((*iter).size()<=3) { iter++; continue; } if((*iter)[(*iter).size()-1]=='s') suffix_s(*iter); iter++; }}void TextQuery::suffix_s(string &word){ string::size_type spos=0; string::size_type pos3=word.size()-3; string suffixes("oussisius"); if(!word.compare(pos3,3,suffixes,spos,3)|| !word.compare(pos3,3,suffixes,spos+6,3)|| !word.compare(pos3+1,2,suffixes,spos+2,2)|| !word.compare(pos3+1,2,suffixes,spos+4,2)) return; string ies("ies"); if(!word.compare(pos3,3,ies)) { word.replace(pos3,3,1,'y'); return ; } string ses("ses"); if(!word.compare(pos3,3,ses)) { word.erase(pos3+1,2); return; } word.erase(pos3+2); if(word[pos3+1]=='\'') word.erase(pos3+1);}void TextQuery::strip_caps(){ vector<string> *words=text_locations->first; vector<string>::iterator iter=words->begin(); vector<string>::iterator iter_end=words->end(); string caps("ABCDEFGHIJKLMNOPQSTUVWXYZ"); while(iter!=iter_end) { string::size_type pos=0; while((pos=(*iter).find_first_of(caps,pos))!=string::npos) (*iter)[pos]=tolower((*iter)[pos]); ++iter; }}void TextQuery::build_word_map(){ word_map=new map<string,loc*>; typedef map<string,loc*>::value_type value_type; typedef set<string> ::difference_type diff_type; set<string>exclusion_set; ifstream infile("exclusion_set"); if(!infile) { static string default_excluded_words[25]= { "the","and","but","that","then","are","been", "can","can't","cannot","could","did","for", "had","have","him","his","her","its","into", "were","whick","when","with","would" }; cerr<<"waring! unable to open word exclusion file!--" <<"using default set\n"; copy(default_excluded_words,default_excluded_words+25, inserter(exclusion_set,exclusion_set.begin() )); } else { istream_iterator<string,diff_type> input_set(infile),eos; copy(input_set,eos, inserter(exclusion_set,exclusion_set.begin() )); } vector<string> *text_words=text_locations->first; vector<location> *text_locs=text_locations->second; register int elem_cnt=text_words->size(); for(int ix=0;ix<elem_cnt;++ix) { string textword=(*text_words)[ix]; if(textword.size()<3||exclusion_set.count(textword)) continue; if(!word_map->count((*text_words)[ix])) { loc *ploc=new vector<location>; ploc->push_back((*text_locs)[ix]); word_map->insert(value_type((*text_words)[ix],ploc)); } else (*word_map)[(*text_words)[ix]]->push_back((*text_locs)[ix]); }}void TextQuery::query_text(){ string query_text; do { cout<<"enter a word against which to search the text.\n" <<"to quit ,enter a single character ==> "; cin>>query_text; if(query_text.size()<2) break; string caps("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); string ::size_type pos=0; while((pos=query_text.find_first_of(caps,pos))!=string::npos) query_text[pos]=tolower(query_text[pos]); if(!word_map->count(query_text)) { cout<<"\nSorry.There are no entries for "<<query_text<<".\n\n"; continue; } loc *ploc=(*word_map)[query_text]; set<short,less<short> > occurrence_lines; loc::iterator liter=ploc->begin(), liter_end=ploc->end(); while(liter!=liter_end) { occurrence_lines.insert(occurrence_lines.end(),(*liter).first); ++liter; } register int size=occurrence_lines.size(); cout<<"\n"<<query_text<<"occurs"<<size<<(size==1?"time:":"times:") <<"\n\n"; set<short>::iterator it=occurrence_lines.begin(); for(;it!=occurrence_lines.end();++it) { int line=*it; cout<<"\t (line"<<line+1<<")"<<(*lines_of_text)[line]<<endl; } cout<<endl; } while(!query_text.empty()); cout<<"OK,bye!\n";}void TextQuery::display_map_text(){ typedef map<string,loc*>map_text; map_text::iterator iter=word_map->begin(), iter_end=word_map->end(); while(iter!=iter_end) { cout<<"word: "<<(*iter).first<<" ("; int loc_cnt=0; loc *text_locs=(*iter).second; loc::iterator liter=text_locs->begin(), liter_end=text_locs->end(); while(liter!=liter_end) { if(loc_cnt) cout<<","; else ++loc_cnt; cout<<"("<<(*liter).first<<","<<(*liter).second<<")"; ++liter; } cout<<")\n"; ++iter; } cout<<endl;}void TextQuery::display_text_locations(){ vector<string> *text_words=text_locations->first; vector<location> *text_locs=text_locations->second; register int elem_cnt=text_words->size(); if(elem_cnt!=text_locs->size()) { cerr<<"oops!internal error:word and position vectors." <<"are of unequal size\n"<<"words: " <<elem_cnt<< " "<<"locs: "<<text_locs->size() <<"--bailing out!\n"; exit(-2); } for(int ix=0;ix<elem_cnt;ix++) { cout<<"word: "<<(*text_words)[ix]<<"\t" <<"location:(" <<(*text_locs)[ix].first<<"," <<(*text_locs)[ix].second<<")" <<"\n"; } cout<<endl;} vc6对standard C++支持不好。 加 .h 没有用, 这个程序用到了 STL可能是VC6.0不支技最新的 STL 吧,想一想 VC6.0 开发的年代(不是发布,发布是在99年,但开发要比发布早几年),比C++标准定案时(98年)还早 微软的编译器是很差的,(对c++98标准支持而言)。borland就好多了。要想用标准的c++写程序,最好抛弃微软。或者等lippman改好了vc再说。lippman,c++ primer 著者,已经加入微软,现任vc7的主架构师。 CxImage::Encode时程序崩溃 关于在区域(HRGN)贴图的问题? 如何用钩子实现这样的功能!!! 多线程调试速度慢到死机问题,100分奉送解决者 请求帮助!求《windows95程序设计》一书的配套代码! 各位高手帮帮忙啊!关于ISTORAGE的使用! =======《磁盘空间预分配问题》======= 为什么RegQueryValueEx();RegSetValueEx();在98下正常,在Server2000上就失效了。 如何在子窗口中读取数据库数据????? 关于Access取值 各位大侠帮帮忙~ 字符串加密你会用什么办法?如果把函数封装在DLL里是否安全?
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <set>#include <fstream>
#include <iostream>
#include <stddef.h>
#include <ctype.h>using namespace std;typedef pair <short,short> location;
typedef vector <location> loc;
typedef vector <string> text1;
typedef pair<text1 *,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> *lines_of_text;
text_loc *text_locations;
map<string,loc* > *word_map;
static string filt_elems;
};
string TextQuery::filt_elems("\",.;:!?)(\\/" );
int main()
{
TextQuery tq;
tq.doit();
tq.query_text();
tq.display_map_text();
}void TextQuery::retrieve_text()
{
std::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!";
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 TextQuery::separate_words()
{
vector<string> *words=new vector<string>;
vector<location> *locations=new vector<location>;
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> *words=text_locations->first;
vector<string>::iterator iter=words->begin();
vector<string>::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;
}
}
void TextQuery::suffix_text()
{
vector<string> *words=text_locations->first;
vector<string>::iterator iter=words->begin();
vector<string>::iterator iter_end=words->end(); while(iter!=iter_end)
{
if((*iter).size()<=3)
{ iter++; continue; }
if((*iter)[(*iter).size()-1]=='s')
suffix_s(*iter);
iter++;
}
}void TextQuery::suffix_s(string &word)
{
string::size_type spos=0;
string::size_type pos3=word.size()-3; string suffixes("oussisius");
if(!word.compare(pos3,3,suffixes,spos,3)||
!word.compare(pos3,3,suffixes,spos+6,3)||
!word.compare(pos3+1,2,suffixes,spos+2,2)||
!word.compare(pos3+1,2,suffixes,spos+4,2))
return;
string ies("ies");
if(!word.compare(pos3,3,ies))
{
word.replace(pos3,3,1,'y');
return ;
} string ses("ses");
if(!word.compare(pos3,3,ses))
{
word.erase(pos3+1,2);
return;
} word.erase(pos3+2);
if(word[pos3+1]=='\'')
word.erase(pos3+1);
}void TextQuery::strip_caps()
{
vector<string> *words=text_locations->first; vector<string>::iterator iter=words->begin();
vector<string>::iterator iter_end=words->end();
string caps("ABCDEFGHIJKLMNOPQSTUVWXYZ");
while(iter!=iter_end)
{
string::size_type pos=0;
while((pos=(*iter).find_first_of(caps,pos))!=string::npos)
(*iter)[pos]=tolower((*iter)[pos]);
++iter;
}
}void TextQuery::build_word_map()
{
word_map=new map<string,loc*>;
typedef map<string,loc*>::value_type value_type;
typedef set<string> ::difference_type diff_type;
set<string>exclusion_set; ifstream infile("exclusion_set");
if(!infile)
{
static string default_excluded_words[25]=
{
"the","and","but","that","then","are","been",
"can","can't","cannot","could","did","for",
"had","have","him","his","her","its","into",
"were","whick","when","with","would"
};
cerr<<"waring! unable to open word exclusion file!--"
<<"using default set\n";
copy(default_excluded_words,default_excluded_words+25,
inserter(exclusion_set,exclusion_set.begin() ));
}
else
{
istream_iterator<string,diff_type> input_set(infile),eos;
copy(input_set,eos,
inserter(exclusion_set,exclusion_set.begin() ));
}
vector<string> *text_words=text_locations->first;
vector<location> *text_locs=text_locations->second;
register int elem_cnt=text_words->size();
for(int ix=0;ix<elem_cnt;++ix)
{
string textword=(*text_words)[ix];
if(textword.size()<3||exclusion_set.count(textword))
continue;
if(!word_map->count((*text_words)[ix]))
{
loc *ploc=new vector<location>;
ploc->push_back((*text_locs)[ix]);
word_map->insert(value_type((*text_words)[ix],ploc));
}
else (*word_map)[(*text_words)[ix]]->push_back((*text_locs)[ix]);
}
}void TextQuery::query_text()
{
string query_text;
do
{
cout<<"enter a word against which to search the text.\n"
<<"to quit ,enter a single character ==> ";
cin>>query_text;
if(query_text.size()<2) break; string caps("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
string ::size_type pos=0;
while((pos=query_text.find_first_of(caps,pos))!=string::npos)
query_text[pos]=tolower(query_text[pos]);
if(!word_map->count(query_text))
{
cout<<"\nSorry.There are no entries for "<<query_text<<".\n\n";
continue;
} loc *ploc=(*word_map)[query_text];
set<short,less<short> > occurrence_lines;
loc::iterator liter=ploc->begin(),
liter_end=ploc->end();
while(liter!=liter_end)
{
occurrence_lines.insert(occurrence_lines.end(),(*liter).first);
++liter;
}
register int size=occurrence_lines.size();
cout<<"\n"<<query_text<<"occurs"<<size<<(size==1?"time:":"times:")
<<"\n\n";
set<short>::iterator it=occurrence_lines.begin();
for(;it!=occurrence_lines.end();++it)
{
int line=*it;
cout<<"\t (line"<<line+1<<")"<<(*lines_of_text)[line]<<endl;
}
cout<<endl;
}
while(!query_text.empty());
cout<<"OK,bye!\n";
}void TextQuery::display_map_text()
{
typedef map<string,loc*>map_text;
map_text::iterator iter=word_map->begin(),
iter_end=word_map->end();
while(iter!=iter_end)
{
cout<<"word: "<<(*iter).first<<" (";
int loc_cnt=0;
loc *text_locs=(*iter).second;
loc::iterator liter=text_locs->begin(),
liter_end=text_locs->end();
while(liter!=liter_end)
{
if(loc_cnt)
cout<<",";
else ++loc_cnt;
cout<<"("<<(*liter).first<<","<<(*liter).second<<")";
++liter;
}
cout<<")\n";
++iter;
}
cout<<endl;
}void TextQuery::display_text_locations()
{
vector<string> *text_words=text_locations->first;
vector<location> *text_locs=text_locations->second; register int elem_cnt=text_words->size();
if(elem_cnt!=text_locs->size())
{
cerr<<"oops!internal error:word and position vectors."
<<"are of unequal size\n"<<"words: "
<<elem_cnt<< " "<<"locs: "<<text_locs->size()
<<"--bailing out!\n";
exit(-2);
}
for(int ix=0;ix<elem_cnt;ix++)
{
cout<<"word: "<<(*text_words)[ix]<<"\t"
<<"location:("
<<(*text_locs)[ix].first<<","
<<(*text_locs)[ix].second<<")"
<<"\n";
}
cout<<endl;
}
borland就好多了。要想用标准的c++写程序,最好抛弃微软。
或者等lippman改好了vc再说。lippman,c++ primer 著者,已经加入微软,现任vc7的主架构师。