簡體   English   中英

不明白為什么C ++程序不產生輸出

[英]Do not understand why C++ program produces no output

我正在進行加速的C ++練習3-3,我一生都無法弄清為什么我的程序不產生輸出。 我什至嘗試在此過程中添加測試提示,但是它沒有給我任何東西。 為什么即使在main for循環之外添加cout語句,它也根本不產生任何輸出?

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <vector>

using std::cout;        using std::cin;
using std::endl;        using std::vector;
using std::sort;        using std::string;

int main() {
    int count = 0;
    string input;
    vector<string> words;
    typedef vector<string>::size_type vec_sz;
    vec_sz size = words.size();

    cout << "Sentence: ";
    while(cin >> input) {
        words.push_back(input);
    }

    for(int i = 0; i < size - 1; i++) {
        for(int j = 0; j < size - 1; j++) {
            if(words[i] == words[j]) {
                ++count;
            }
        }
        cout << "The word " << words[i] << " appears " << count << " times." << endl;
    }
    return 0;
}

您可以檢查size == 0 因此,這兩個for循環都沒有任何迭代。

要修復它,您需要移動vec_sz size = words.size(); 低幾行。

試試這個命令:

cout << "Sentence: ";
while(cin >> input) {
    words.push_back(input);
}

vec_sz size = words.size();

for循環中,邏輯也有一個錯誤。 您每次都需要重置count (只需在內部循環之前添加count = 0;就可以得到正確的結果)。

您可以在此處檢查其工作方式: http : //ideone.com/T2cPcH (帶有count初始化錯誤)

將任何內容插入之前,先讀取size變量。 因此,在您閱讀時,其值為0。 如果要使其到達底部的for循環,則該循環將不運行任何迭代0 < (0 - 1)為假。

您可以刪除size變量。 vector::size是一個恆定時間的操作。 調用它沒有任何開銷。

我也改變了你從cin讀取行的方式。 現在它將使用std::getline讀取該行。 該行可能在一行上包含多個單詞,因此它使用stringstream從該行中讀取每個單詞並將其插入向量中。

還有一個稱為multiset容器,它在計數單詞方面可能會更好地滿足您的需求。

更好的方法是使用unordered_dict ,該值可用於保存每個字符串的計數。 之所以起作用,是因為當您將一個新條目插入int unordered_map它的int值將默認為0,因此您可以始終安全地對其執行增量操作。

下面的例子。

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <unordered_map>

// don't do this in a header file!!
using namespace std;

void basic()
{
    string input;
    vector<string> words;

    cout << "Sentence: ";

    if(std::getline(std::cin, input))
    {
        istringstream ss(input);
        string word;

        while(ss >> word){
            words.push_back(word);
        }
    }

    for(const auto& word: words) {
        int count = 0;
        for(const auto& word2: words) {
            if(word == word2) {
                ++count;
            }
        }
        cout << "The word " << word << " appears " << count << " times.\n";
    }
}

void better()
{
    multiset<string> ms;
    string input;

    cout << "Sentence: ";

    if(std::getline(std::cin, input))
    {
        istringstream ss(input);
        string word;

        while(ss >> word){
            ms.insert(word);
        }
    }

    for(auto it = ms.begin(), end = ms.end(); 
        it != end; 
        it = ms.upper_bound(*it))
    {
        cout << *it << "'count=" << ms.count(*it) << '\n';
    }
}

void best()
{
    unordered_map<string, int> counter;
    string input;

    cout << "Sentence: ";

    if(std::getline(std::cin, input))
    {
        istringstream ss(input);
        string word;

        while(ss >> word){
            counter[word]++;
        }
    }

    for(const auto& it: counter)
    {
        cout << it.first << "'count=" << it.second << '\n';
    }
}

int main() 
{
    //basic();
    //better(); 
    best();   
}

暫無
暫無

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

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