小编use*_*094的帖子

为什么以下C++程序打印字符串的ascii值而不是字符?

该程序应该存储标准输入流中给出的每个单词并计算它们的出现次数.结果应该按顺序打印,然后按顺序打印.据我所知,该程序正在工作,但字符串打印为字符的ASCII值而不是字符本身.怎么了?

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
#include <algorithm>

std::string get_word();

int main()
{
    std::vector<std::string> words;
    std::string word;

    while (std::cin.good()) {
        word = get_word();

        if (word.size() > 0)
            words.push_back(word);
    }

    std::sort(words.begin(), words.end());

    unsigned n, i = 0;

    while (i < words.size()) {
        word = words[i];
        n = 1;

        while (++i < words.size() and words[i] == word)
            ++n;

        std::cout << word << ' ' << n << std::endl;
    }
}


std::string get_word()
{
    while (std::cin.good() and !std::isalpha(std::cin.peek())) …
Run Code Online (Sandbox Code Playgroud)

c++ printing string

4
推荐指数
1
解决办法
1215
查看次数

标签 统计

c++ ×1

printing ×1

string ×1