【问题标题】:unique words from a file c++文件c ++中的唯一单词
【发布时间】:2020-07-10 13:39:09
【问题描述】:

已经 3 天了,我只是无法确定程序有什么问题,程序应该逐字比较,而不是只比较一个字符和字符,它的显示就像我有像 (aaa bbb cc dd) 这样的单词结果它的打印是 ab,如果我放段落来比较它唯一比较的几个字符,那么句子文件也是一样的,请帮助我

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

using namespace std;

int main()    
{
    ifstream myfile("unique.text");

    int count = 0;

    string temp;

    string a;    

    int i,j;

    while(getline(myfile,temp))
    {
        for(i=0 ; i < sizeof(temp); i++)
        {
            for(int j = 0; j < i; j++)
            {
                if (temp[i] == temp[j])
                    break;
            }   

            if (i == j)
                cout << temp [i] <<" , ";
        }

        myfile.close (); 
   }

【问题讨论】:

  • 欢迎来到 SO。请点击“编辑”并修复您的代码,有很多不应该存在的空格字符。

标签: c++ string file fstream


【解决方案1】:

你有几个问题

temp 的类型为 stringsizeof 不是确定string 长度的方法(它用于确定int 中的字节数等)。你想要:

temp.length()

其次,对字符串 (temp[n]) 进行索引会为您提供第 n 个字符,而不是第 n 个单词。

您可以通过添加第三个分隔符参数将getline拆分为单词:

getline (myfile, temp, ' '))

【讨论】:

  • 非常感谢您的回复先生,我已经按照您的要求完成了,但现在它打印了我放入文件中的每一个字
  • 这不仅仅是改变那一行的情况。这就是如何阅读单个单词。你如何处理它们是由你来解决的。
  • @Mightybendar 如果你真的想感谢人们,正确的做法是至少支持他们的答案!接受你觉得是最好的答案。这就是它的工作原理。
【解决方案2】:

所以,您的代码中存在一些错误。

混合字符和字符串,在 while 循环中关闭文件并且不存储最后的单词。

一个建议。在你写代码之前,为你想做的事情写 cmets。

意思是,在开始编码之前进行设计。这很重要。

对于本帖标题中您手头的问题:

文件 c++ 中的唯一单词

我准备了 3 种不同的解决方案。第一个只是使用非常简单的结构。第二个是使用std::vector。并且,第三种是使用C++算法库的C++解决方案。

请看:

简单但冗长

不推荐,因为我们不应该对拥有的内存使用原始指针,也不应该使用new

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

const std::string fileName{ "unique.text" };


unsigned int numberOfWords() {

    // Here we will count the number of words in the file
    unsigned int counter = 0;

    // Open the file. File must not be already open
    std::ifstream sourceFileStream(fileName);

    // Check, if we could open the file
    if (sourceFileStream) {

        // Simply read all words and increment the counter
        std::string temp;
        while (sourceFileStream >> temp) ++counter;
    }
    else {
        // In case of problem
        std::cerr << "\nCould not open file '" << fileName << "'\n";
    }
    return counter;
}

int main() {

    // Get the number of words in the source file
    unsigned size = numberOfWords();

    // Allocate a dynamic array of strings. Size is the count of the words in the file
    // Including doubles. So we will waste a little bit of space
    std::string* words = new std::string[size+1];

    // Open the source file
    std::ifstream sourceFileStream(fileName);

    // Check, if it could be opened
    if (sourceFileStream) {

        // We will read first into a temporary variable
        std::string temp;
        // Her we will count number of the unique words
        unsigned int wordCounter = 0;

        // Read all words in the file
        while (sourceFileStream >> temp) {

            // We will search, if we have read alread the word before. We assume NO for the beginning
            bool wordIsAlreadyPresent = false;

            // Go through all alread read words, and check, if the just read word is already existing
            for (unsigned int i = 0; i < wordCounter; ++i) {

                // Check, if just read word is already in the word array
                if (temp == words[i]) {

                    // Yes it is, set flag, and stop the loop.
                    wordIsAlreadyPresent = true;
                    break;
                }
            }
            // if the word was not already there
            if (! wordIsAlreadyPresent) {
                // Then add the just read temporary word into our array
                words[wordCounter] = temp;

                // And increment the counter
                ++wordCounter;
            }
        }

        // Show all read unique words
        for (unsigned int i = 0; i < wordCounter; ++i) {
            std::cout << words[i] << "\n";
        }
    }
    else { // In case of error
        std::cerr << "\nCould not open file '" << fileName << "'\n";
    }
    delete[] words;
}

  1. 使用向量。已经更紧凑,可读性更好
#include <iostream>  
#include <fstream>
#include <string>
#include <vector>

const std::string fileName{ "unique.text" };

int main() {

    // Open the source file
    std::ifstream sourceFileStream(fileName);

    // Check, if the source file is oepen
    if (sourceFileStream) {

        // Temporary string for holding just read words
        std::string temp;

        // In this vector we will store all unique words
        std::vector<std::string> words;

        // Read all words from the source file
        while (sourceFileStream >> temp) {

            // We will search, if we have read alread the word before. We assume NO for the beginning
            bool wordIsAlreadyPresent = false;

            // Go through all alread read words, and check, if the just read word is already existing
            for (unsigned int i = 0; i < words.size(); ++i) {
                // Check, if just read word is already in the word vector
                if (temp == words[i]) {

                    // Yes it is, set flag, and stop the loop.
                    wordIsAlreadyPresent = true;
                    break;
                }
            }
            // if the word was not already there
            if (not wordIsAlreadyPresent) {

                // Then add the just read temporary word into our array
                words.push_back(temp);
            }
        }
        for (unsigned int i = 0; i < words.size(); ++i) {
            std::cout << words[i] << "\n";
        }
    }
    else {
        std::cerr << "\nCould not open file '" << fileName << "'\n";
    }
}

还有 3.,更高级的 C++ 编程。只需很少的几行代码和优雅的代码。

但是对于初学者来说太难理解了。

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

const std::string fileName{ "unique.text" };

int main() {

    // Open the source file and check, if it could be opend and there is no failure
    if (std::ifstream sourceFileStream(fileName); sourceFileStream) {

        // Read all words (everything delimited by a white space) into a set
        std::set words(std::istream_iterator<std::string>(sourceFileStream), {});

        // Now we have a set with all unique words. Show this on the screen
        std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
    }
    // If  we could not open the source file
    else {
        std::cerr << "\nCould not open file '" << fileName << "'\n";
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多