繁体   English   中英

需要读取c++中的连续空格作为一个单词

[英]Need to read consecutive spaces as a word in c++

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

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++;
    }
}

您可能知道, 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('\'s 不在文件中,它们显示行结束的位置):

I   am.\
  hello fellow  friend    \

Output:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM