这个迭代器声明有什么问题

What's wrong with this iterator declaration

本文关键字:什么 问题 声明 迭代器      更新时间:2023-10-16

我在我的类中有一个方法,它是:

void TextQuery::build_word_map()
{
   word_map = new map<string,loc*,less<string>,allocator<string> >;
   typedef map<string,loc*,less<string>,allocator<string> >::value_type value_type;
   typedef set<string,less<string>,allocator<string> >::difference_type  diff_type;
   set<string,less<string>,allocator<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","which","when","with","would"};
      cerr << "warning! unable to open word exclusion file! -- "  << "using     default setn";
      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<string> > *text_words = text_locations->first;
   vector<location,allocator<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,allocator<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] );
    }
}

,当我试图构建它时,我被告知不可能将"std::ifstream"转换为"std::basic_istream<_Elem,_Traits> &"?这意味着什么,我该做什么(除了深入研究迭代器,也就是;-))?

您有错误,因为您使用diff_type作为second模板参数。如果你想使用diff_type,你应该正确地声明istream_iterator,声明应该是

istream_iterator<string, char, std::char_traits<char>, diff_type>

既然istream_iterator真的是

template< class T,
          class CharT = char,
          class Traits = std::char_traits<CharT>,
          class Distance = std::ptrdiff_t >
class istream_iterator

你可以在这里看到