簡體   English   中英

在C ++中計算文本文件中的相同字符串/單詞

[英]Counting same string/word in a text file in C++

我試圖從C ++中的文本文件計算相同的字符串/單詞。

This is my text file
one two three two
test testing 123
1 2 3

這是我的主程序

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, const char** argv)
{
    int counter = 0;
    int ncounter = 0;
    string str;
    ifstream input(argv[1]);

    while (getline(input, str)) 
    {
        if(str.find("two") != string::npos){counter++;}
        if(str.find('\n') != string::npos){ncounter++;}

        cout << str << endl; //To show the content of the file
    }

    cout << endl;
    cout << "String Counter: " << counter << endl;
    cout << "'\\n' Counter: " << ncounter << endl;

    return 0;
}

我正在使用.find()函數查找字符串。 當我插入一個不存在的單詞時,它不算在內。 當我插入“兩個”一詞時,它會計數,但只有一次。

怎么沒算兩次呢?

對於回車符(或回車行; \\ n),它不能計數。 這是為什么?

因為這兩個在同一行上,並且您僅在該行中搜索一個子字符串。
您找不到'\\ n',因為getline函數讀取的行一直到不包含'\\ n'。

為什么不使用std::multiset存儲單詞呢? 它將為您進行計數 ,並且可以在一行中完成將文件讀入其中的操作:

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

int main(int argc, const char** argv)
{
    // Open the file
    std::ifstream input(argv[1]);

    // Read all the words into a set
    std::multiset<std::string> wordsList = 
        std::multiset<std::string>( std::istream_iterator<std::string>(input),
                                    std::istream_iterator<std::string>());

    // Iterate over every word
    for(auto word = wordsList.begin(); word != wordsList.end(); word=wordsList.upper_bound(*word))
        std::cout << *word << ": " << wordsList.count(*word) << std::endl;

    // Done
    system("pause");
    return 0;
}

注意最后for部分word=wordsList.upper_bound(*word) 從技術上講,您可以將其切換為簡單的word++ (然后將其簡化for(auto word: wordList )會更好。它可以確保集合中的每個值僅輸出一次。

它還會列出單詞本身,而無需像現在在當前while循環中那樣進行操作。

最好的選擇是閱讀每一行,然后沿空白標記,以便您可以單獨檢查每個單詞。

我懷疑我們在這里談論的是一項家庭作業,所以我最好的答案是將您引導至std :: strtok的C ++參考: http : //en.cppreference.com/w/cpp/string/byte/strtok

暫無
暫無

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

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