2017-05-28 55 views
-4
int main() 
{ 
    map<string, int> M; 
    vector<string> V; 
    set<string> S; 

    ifstream inFile("sample_doc.txt"); 
    copy(istream_iterator<string>(inFile), istream_iterator<string>(), back_inserter(V)); 
    ifstream inFile2("stopwords.txt"); 
    copy(istream_iterator<string>(inFile2), istream_iterator<string>(), inserter(S, S.begin())); 

    for_each(V.begin(), V.end(), [&](string & s){ S.count(s) == 0 ? M[s]++ : true; }); 
} 

在for_each語句中,當我傳遞上面的lambda函數時,它給了我下面的錯誤。沒有匹配調用的函數,如何解決?

error: no matching function for call to ‘for_each(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, main()::__lambda0)’ 
    for_each(V.begin(), V.end(), [&](string & s){ S.count(s) == 0 ? M[s]++ : true; }); 

有人能告訴我如何解決它嗎?非常感謝。

+0

包括''標題。 – 2017-05-28 05:03:57

+0

嗨,我做了包括 zeyuxie

+0

替換'和'的std :: for_each' – TheThirdOne

回答

0

這裏建立在VS2017上,也許你忘記了一些標題?

#include <map> 
#include <algorithm> 
#include <set> 
#include <iterator> 
#include <string> 
#include <vector> 
#include <iostream> 
#include <fstream> 

void foo() 
{ 
    using namespace std; 
    map<string, int> M; 
    vector<string> V; 
    set<string> S; 

    ifstream inFile("sample_doc.txt"); 
    copy(istream_iterator<string>(inFile), istream_iterator<string>(), back_inserter(V)); 
    ifstream inFile2("stopwords.txt"); 
    copy(istream_iterator<string>(inFile2), istream_iterator<string>(), inserter(S, S.begin())); 

    for_each(V.begin(), V.end(), [&](string & s) { S.count(s) == 0 ? M[s]++ : true; }); 
}