【发布时间】:2015-11-24 22:59:51
【问题描述】:
我被一个问题难住了一段时间。在将文本文件输入地图容器之前,我似乎无法检查包含一组排除单词的文本文件。我尝试了很多事情,但似乎无法解决它。我是 C++ 新手,刚开始学习 STL 和容器。
using namespace std;
//checking I know is wrong but I do not know how to compare the pair with the set.
bool checking(pair<string, int> const & a, set<string> const &b) {
return a.first != b;
}
void print(pair<string, int> const & a) {cout << a.first << " " << a.second << endl;}
int main() {
ifstream in("document.txt");
ifstream exW("excluded.txt");
map<string, int> M;
set<string> words;
copy(istream_iterator<string>(exW),
istream_iterator<string>(),
inserter(words, begin(words)));
//Need to exlclude certain words before copying into a Map
// CAN NOT USE FOR LOOP
//I cant seem to get the predicate right.
copy_if(istream_iterator<string>(in),
istream_iterator<string>(),
[&](const string & s) { M[s]++;},
checking);
for_each(begin(M),
end(M),
[](pair<string, int> const & a)
{
cout << a.first << " " << a.second << endl;
}
);
return 0;
}
任何提示或建议都很棒!
【问题讨论】:
-
您到底想达到什么目的?您不能将字符串与集合“比较”,它们完全代表不同的概念。您是否尝试查看该字符串是否属于该集合?
-
@vsoftco 我正在尝试读取 sample.txt 和 copy_if 不排除单词到地图容器中。
-
如果你想复制字符串,
copy_if谓词需要返回true,如果不是false。
标签: c++ c++11 stl containers