首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++primer3rd cn P263-P264第6章抽象容器类型最后完整程序中TextQuery类的成员函数。其中,红色部分是不是有错?小弟我如何也看不懂

2012-05-05 
c++primer3rd cn P263-P264第6章抽象容器类型最后完整程序中TextQuery类的成员函数。其中,红色部分是不是有

c++primer3rd cn P263-P264第6章抽象容器类型最后完整程序中TextQuery类的成员函数。其中,红色部分是不是有错?我怎么也看不懂。
void 
TextQuery:: 
build_word_map() 

  word_map = new map< string, loc*, less<string>, allocator >; 
 
 typedef map<string,loc*,less<string>,allocator>::value_type 
  value_type; 
 
 typedef set<string,less<string>,allocator>::difference_type 
  diff_type; 
 
 set<string,less<string>,allocator> 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","which","when","with","would" 
  }; 
  cerr << "warning! 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,allocator> *text_words = text_locations ->first; 
  vector<location,allocator> *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,allocator>; 
  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] ); 
 } 




上面代码摘自 c++primer3rd第三版 pdf格式第263页至264页(第6章抽象容器类型最后完整程序中TextQuery类的成员函void 
TextQuery:: build_word_map() 
其中,红色部分(也就是下面一行
istream_iterator< string, diff_type >
)这半个语句是不是有错?我怎么也看不懂。调试也不行。只有把后面的(, diff_type)去掉才能调试成功,所以我猜是不是书上写错了?去掉 (, diff_type)后对程序有没有影响?有什么影响?
在几个地方都看见有这种说法,说 istream_iterator 只支持单一类型,是这样吗?我猜,如果真是只支持单一类型,那么应该是书上写错了。到现在我可以确定书上有几处错了,但这里我不知道,请高手指教。
请不要叫我百度。我百度了很多次了。 也请不要复制粘贴。谢谢。

[解决办法]
你看看diff_type,有没有定义了,没有定义的话,你可以typedef一下。
如果不传,默认是char。
[解决办法]
默认的即可。diff_type的实际类型一般是int,主要用来两个指针之差!
[解决办法]
从题目意思看,很明显是如果!infile为false的话那么需要你从终端输入数据

热点排行