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 :(得分: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; });
}