2013-04-05 59 views
0

我有一個方法,在我的課,那就是:有什麼不對的迭代聲明

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的「into」std :: basic_istream < _Elem,_Traits> &「? 這是什麼意思,我該怎麼做(除了深入研究迭代器,那是;-))?

+0

你能給出確切的錯誤? – 2013-04-05 11:39:21

+0

請你給我們一個最小的崩潰代碼,以及錯誤出現的位置? – lucasg 2013-04-05 11:41:18

+0

你的分配器是錯誤的類型。 – juanchopanza 2013-04-05 11:41:31

回答

3

您有錯誤,因爲您使用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

+0

實際工作)謝謝。有關如何避免將來出現此類錯誤的任何提示? – Chiffa 2013-04-05 11:48:19

+1

@Chiffa閱讀文檔,使用前。 – ForEveR 2013-04-05 11:48:53