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

标签 c++ istream-iterator

我的类(class)有一个方法,这里是:

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 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<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

如你所见here

关于c++ - 这个迭代器声明有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15832945/

相关文章:

c++ - 使用 istream_iterator 时如何忽略最后的空行

java - Java 中的“导入”与 C/C++ 中的 '#include'

c++ - 无法从 basic_stringstream<char8_t> 读取 char8_t

c++ - 在 cpp 中计算数字的 n 次根时答案错误

c++ - 具有取决于整数模板的参数的构造函数的模板参数推导技巧

c++ - 使用 for_each 和 istream_iterator 遍历 C++ 中的文本文件以查找文件名

c++ - 构造函数中的静态变量,有什么缺点或副作用吗?

c++ - 我可以使用 istream_iterator<char> 将一些 istream 内容复制到 std::string 中吗?

c++ - istream_iterator 的初始化导致 ifstream.fail() 被设置

c++ - std::istream_iterator,内存消耗