【问题标题】:Need to read consecutive spaces as a word in c++需要在c ++中将连续空格读取为单词
【发布时间】:2021-11-18 15:29:11
【问题描述】:

我需要从文件中读取单词并将它们存储在字符串对象数组中。但是,问题是我需要将多个空格视为一个单词。 例如,

I   am.

这只是两个单词,它们之间有三个三个空格。但是,我需要证明这一点:

arr[0]="I"
arr[1]=" "
arr[2]=" "
arr[3]="am"

谁能帮帮我?这是我写的代码:

strSet = new std::string[noStrings];
file.open(filename);
if (file) {
    unsigned int i = 0;
    while (file>>strSet[i])
    {
        i++;
    }
}

【问题讨论】:

  • 你试过一次只看一个字符吗?
  • 三个空格怎么变成两个单空格字符串?两个非空白单词之间的单个空格应该发生什么?如果你能把逻辑解释清楚,你就可以编程了。
  • "这是我写的代码:" -- 这是一个起点。为什么不是终点?

标签: c++ string loops file-io


【解决方案1】:

您可能知道,file >> string 确实会忽略任何空白字符逐字阅读。

据我所知,记录空格的最简单方法是读取整行,然后再解析该行。 这是我的实现:

#include <iostream>
#include <fstream>
#include <vector>

std::vector<std::string> readWords(std::ifstream& file, size_t reservedSize = 0) {
        std::string buffer;
        std::vector<std::string> words;
        words.reserve(reservedSize);

        //while there is still something to read
        while (file.good()) {
                //read a line
                std::getline(file, buffer);
                for(size_t start = 0, end = 0; start < buffer.length();) {
                        //if a space is encountered
                        if (buffer[start] == ' ') {
                                //ignore the first space
                                ++start;
                                end = start;
                                //count how many consecutive spaces
                                while(end < buffer.length() && buffer[end] == ' ')
                                        ++end;
                                //save spaces as separete strings
                                for (; start != end; ++start)
                                        words.emplace_back(" ");
                        }
                        else {
                                //skip all whitespace characters, except ' '
                                while(start < buffer.length() &&
                                        buffer[start] == '\v' &&
                                        buffer[start] == '\r' &&
                                        buffer[start] == '\n')
                                        ++start;
                                //count how many consecutive non whitespace characters
                                while(end < buffer.length() && !isspace(buffer[end])) ++end;
                                //add to array
                                words.push_back(buffer.substr(start, end - start));
                                //don't forget to increment start to avoid
                                //infinite loops
                                start += end - start;
                        }
                }
        }
        //return the array
        return words;
}



int main() {
        std::ifstream stream("input.txt");
        auto words = readWords(stream);
        for (const auto& e : words) {
                std::cout << '\'' << e << "'\n";
        }
}

文件 input.txt('\' 不在文件中,它们显示行结束的位置):

I   am.\
  hello fellow  friend    \

输出:

'I'
' '
' '
'am.'
' '
'hello'
'fellow'
' '
'friend'
' '
' '
' '

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    相关资源
    最近更新 更多