簡體   English   中英

STL容器和算法C ++

[英]STL containers and algorithm C++

我被一個問題困擾了一段時間。 在將文本文件輸入到地圖容器之前,我似乎無法檢查帶有一組排除單詞的文本文件。 我嘗試了很多事情,但似乎無法解決。 我是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;
}

任何提示或建議詞都很棒!

我會使用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::coutstd::copy_if 當然,您可以在某個容器中使用迭代器(例如,您的std::map )。 還要注意謂詞以std::string作為輸入(您要驗證的內容),並檢查它是否屬於排除單詞的std::set ,並返回bool 同樣, words需要在lambda內部捕獲。 我通過引用將其捕獲,因此您不會再獲得其他副本。

如果您需要使用標准算法而不是循環,那么我建議您使用標頭<numeric>聲明的標准算法std::accumulate

這是一個演示程序。 我使用的是字符串流而不是文件。

#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 );

或者如@TC所指出的,更簡化的方法是使用標准算法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;                     
}

通常,可以使用不同的算法以幾種方式完成相同的任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM