undefined undefined
undefined undefined

Reputation: 11

How to read only certain range of symbols from std::basic_ostream (e.g. std::cin)

#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?

Upvotes: 1

Views: 30

Answers (0)

Related Questions