从 C++ 中的映射中删除文本文件的单词,无需循环

remove words of a text file from a map in C++ without loop

提问人:Frank 提问时间:11/26/2015 最后编辑:Frank 更新时间:11/26/2015 访问量:454

问:

我尝试制作一个集合来存储文本文件的某些单词。然后我想从我已经编好的地图中删除这些词。我已经成功地制作了一个集合来存储这些单词,但我无法将它们从地图中删除。此外,我不能使用循环语句(如 for 循环或 while 循环)。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <list>

  ifstream stop_file( "remove_words.txt" );
  ofstream out( "output.txt" );

  set <string> S;

  copy(istream_iterator<string>(stop_file), 
       istream_iterator<string>(),
       inserter(S, begin(S)));

         //copy: copy from text file into a set

  remove_if(M.begin(), M.end(), S);

        //remove: function I try to remove words among words stored in a map
        //map I made up is all set, no need to worry
C++ STL IOSTREAM fstream 删除-if

评论


答:

0赞 philm 11/26/2015 #1

你能提供你的地图的声明吗?

例如,如果地图是,您可以执行如下操作:map<string, int>

for (string & s : set)
{
    map.erase(s);
}

使用 for_each 将如下所示:

std::for_each(set.begin(), set.end(), 
    [&map](const std::string & s) { map.erase(s); });

此外,使用递归,可以在完全没有循环的情况下进行删除

template <typename Iter>
void remove_map_elements(
    std::map<std::string, int> & map,
    Iter first,
    Iter last)
{
    if (first == last || map.empty())
        return;

    map.erase(*first);
    remove_map_elements(map, ++first, last);
}

你称之为

 remove_map_elements(map, set.begin(), set.end());

评论

0赞 11/26/2015
这应该是因为 是一个常量迭代器。如果没有 ,它就无法编译。const string&std::set::iteratorconst
0赞 Frank 11/26/2015
是的,谢谢。我的地图是<string,int> 但是我真的需要另一种方法来删除单词而不编写 for 循环。不过,for_each适用。
0赞 Neil Kirk 11/26/2015
@Dorothy 为什么不能使用 for 循环?它必须在某个时候做某种循环。此外,最有效的方法是要求您编写自己的 for 循环,以考虑集合和映射的顺序。朴素的解决方案将在每次擦除时执行完整的日志查找。
0赞 Frank 11/26/2015
@NeilKirk 对不起,我没有提到。实际上这是一个作业,我正在学习高级C++循环,所以我不能。
0赞 mvd 11/26/2015 #2

如果我理解正确的话,你需要这样的东西:

  std::map< std::string, int > m = {
    { "word1", 1 },
    { "word2", 2 },
    { "word3", 3 },
    { "word4", 4 }
  };

  std::set< std::string > wordsToRemove = { "word2" };

  std::for_each( 
    wordsToRemove.begin(), 
    wordsToRemove.end(), 
    [&m] ( const std::string& word )   
    { 
      m.erase( word );  
    } 
  );