首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不理解为什么C++程序不产生输出

我不理解为什么C++程序不产生输出
EN

Stack Overflow用户
提问于 2017-03-07 12:03:29
回答 2查看 589关注 0票数 0

我正在做加速C++练习3-3,但我终生都搞不明白为什么我的程序没有输出。我甚至尝试在此过程中添加test couts,但它没有给我任何帮助。为什么当我在主for循环之外添加cout语句时,它根本不会产生任何输出呢?

代码语言:javascript
复制
#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;
}
EN

回答 2

Stack Overflow用户

发布于 2017-03-07 12:06:17

您可以查看该size == 0。因此,在两个for-loops中都没有任何迭代。

要解决这个问题,您需要将行vec_sz size = words.size();向下移动几行。

请尝试此订单:

代码语言:javascript
复制
cout << "Sentence: ";
while(cin >> input) {
    words.push_back(input);
}

vec_sz size = words.size();

此外,您的for-loops中存在错误的it逻辑。每次都需要重置count (只需在内部循环之前添加count = 0;,以获得正确的结果)。

你可以在这里检查它是如何工作的:http://ideone.com/T2cPcH (错误的初始化count)

票数 2
EN

Stack Overflow用户

发布于 2017-03-08 07:16:16

在之前读取size变量,然后再向其中插入任何内容。因此,在你读取它的时候,它的值是0。如果它到达底部的for循环,该循环将不会运行迭代,0 < (0 - 1)为false。

您可以去掉size变量。vector::size是一个恒定的时间操作。调用它不会产生任何开销。

此外,我还更改了从cin中读取代码行的方式。现在,它将使用std::getline读取行。一行中可能有多个单词,因此它使用stringstream从一行中读取每个单词,并将其插入到向量中。

还有另一个名为multiset的容器,它在计算单词方面可能更适合您的需求。

一种更好的方法是使用unordered_dict,它可以用来保存每个字符串的计数。它之所以有效,是因为当您在unordered_map中插入一个新条目时,它的int值将缺省为0,因此您可以安全地始终对其应用递增操作。

下面是一些例子。

代码语言:javascript
复制
#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();   
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42639995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档