STL容器和算法C ++

时间:2015-11-24 22:59:52

标签: c++ c++11 stl containers

我有一段时间难以接受问题。在将文本文件输入到地图容器中之前,我似乎无法使用一组排除的单词检查文本文件。我尝试了很多东西,但它似乎无法解决它。我是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;
}

任何提示或建议都很棒!

2 个答案:

答案 0 :(得分:2)

我会这样做,使用lambda表达式作为测试,所以这可以让你开始:

#include <set>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

int main() 
{
    ifstream in("document.txt");
    ifstream exW("excluded.txt");

    set<string> words{istream_iterator<string>(exW),{}}; // here we store the excluded words

    copy_if(istream_iterator<string>(in),
            istream_iterator<string>(), // can also use just {} instead
            ostream_iterator<string>(std::cout," "), // output to std::cout
            [&words](const std::string& word) // this is how the predicate should look
            {
                return words.find(word) == words.end(); // true if not found
            }
            );
}

请注意,我直接输出到std::cout中的std::copy_if。您当然可以将迭代器用于某个容器(例如,您的std::map)。还要注意谓词将std::string作为输入(这是您验证的内容)并检查它是否属于被排除单词的std::set,并返回bool。同样需要在lambda中捕获words。我通过引用捕获它,因此您不会得到额外的副本。

答案 1 :(得分:0)

如果您需要使用标准算法而不是循环,那么我可以建议使用标题std::accumulate中声明的标准算法<numeric>

这是一个示范程序。而不是我使用字符串流的文件。

#include <iostream>
#include <set>
#include <map>
#include <string>
#include <sstream>
#include <numeric>
#include <iterator>

int main( void )
{
    std::istringstream exclude( "two four six" );
    std::set<std::string> words( ( std::istream_iterator<std::string>( exclude ) ),
                                 std::istream_iterator<std::string>() ); 

    for ( const auto &t : words ) std::cout << t << ' ';
    std::cout << std::endl;

    std::cout << std::endl;

    std::map<std::string, int> m;

    std::istringstream include( "one two three four five six five four one one" );

    std::accumulate( std::istream_iterator<std::string>( include ),
                     std::istream_iterator<std::string>(),
                     &m,
                     [&]( std::map<std::string, int> *acc, const std::string &t )
                     {
                         if ( !words.count( t ) ) ++( *acc )[t];
                         return acc;
                     } );

    for ( const auto &p : m ) std::cout << p.first << '\t' << p.second << std::endl;                     
}

程序输出

four six two 

five    2
one 3
three   1

为了程序的可读性,lambda定义可以放在算法调用之外。例如

auto add_if_not_in_set = [&]( std::map<std::string, int> *acc, const std::string &t )
{
    if ( !words.count( t ) ) ++( *acc )[t];
    return acc;
};

//...

std::accumulate( std::istream_iterator<std::string>( include ),
                 std::istream_iterator<std::string>(),
                 &m, add_if_not_in_set );

或者 @ T.C。指出更简化的方法是使用标准算法std::for_each

例如

#include <iostream>
#include <set>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>

int main( void )
{
    std::istringstream exclude( "two four six" );
    std::set<std::string> words( ( std::istream_iterator<std::string>( exclude ) ),
                                 std::istream_iterator<std::string>() ); 

    for ( const auto &t : words ) std::cout << t << ' ';
    std::cout << std::endl;

    std::cout << std::endl;

    std::map<std::string, int> m;


    std::istringstream include( "one two three four five six five four one one" );

    std::for_each( std::istream_iterator<std::string>( include ),
                   std::istream_iterator<std::string>(),
                   [&m, &words]( const std::string &s )
                   {
                       if ( !words.count( s ) ) ++m[s];
                   } );

    for ( const auto &p : m ) std::cout << p.first << '\t' << p.second << std::endl;                     
}

通常可以使用不同的算法以多种方式完成相同的任务。:)