如何仅读取std :: basic_ostream的某些符号(例如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;
}
是否有某种方法可以从流中读取数据,同时忽略某些字符(反之亦然,仅阅读某些字符)?我知道如何“手动”(上面的示例)进行操作,但是我想使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论