如何仅读取std :: basic_ostream的某些符号(例如std :: cin)

发布于 2025-01-27 10:19:01 字数 782 浏览 2 评论 0原文

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

int main () {
    std::vector<std::string> words{};
    std::string word;
    char letter;
    while (std::cin.get(letter) && letter != '\n') {
        if ('a' <= std::tolower(letter) && std::tolower(letter) <= 'z') {
            word.push_back(letter);
        } else if (!word.empty()) {
            words.push_back(word);
            word.clear();
        }
    }
    words.push_back(word);

    std::ostream_iterator<std::string> out(std::cout, " ");
    std::copy(words.begin(), words.end(), out);

    return 0;
}

是否有某种方法可以从流中读取数据,同时忽略某些字符(反之亦然,仅阅读某些字符)?我知道如何“手动”(上面的示例)进行操作,但是我想使用C ++标准库的所有功能。也许有一些特殊的溪流操纵器?

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

int main () {
    std::vector<std::string> words{};
    std::string word;
    char letter;
    while (std::cin.get(letter) && letter != '\n') {
        if ('a' <= std::tolower(letter) && std::tolower(letter) <= 'z') {
            word.push_back(letter);
        } else if (!word.empty()) {
            words.push_back(word);
            word.clear();
        }
    }
    words.push_back(word);

    std::ostream_iterator<std::string> out(std::cout, " ");
    std::copy(words.begin(), words.end(), out);

    return 0;
}

Is there some way to read data from the stream, while ignoring certain characters (or vice versa, reading only certain ones)? I know how to do it "manually" (example above), but I want to use all the functionality of the C++ standard library. Maybe there are some special stream manipulators?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文