伪 istream 指针返回
Pseudo-istream pointer return
我一直在学习Stroustrup的编程和原理来自学c ++ 11。在第11章中,他描述了一个程序,该程序可以从输入流中删除(变成空格)任何不需要的字符。因此,例如,我可以设置一个字符串来保存字符"!"和"."。然后我可以输入 dog! food
并接收输出dog food
。
但是,我不明白字符串如何word
在主要
int main ()
{
Punct_stream ps {cin};
ps.whitespace(";:,.?!()"{}<>/&$@#%^*|~");
ps.case_sensitive(false);
cout<<"Please input words."<<"n";
vector<string> vs;
for (string word; ps>>word;)// how does word get assigned a string? {
vs.push_back(word);
}
sort(vs.begin(), vs.end());
for (int i = 0; i<vs.size(); ++i) {
if (i==0 || vs[i]!=vs[i-1]) cout<<vs[i]<<"n";
}
}
通过重载定义>>赋值。
Punct_stream& Punct_stream::operator>>(string& s)
{
while (!(buffer>>s)) {
if (buffer.bad() || !source.good()) return *this;
buffer.clear();
string line;
getline(source,line); // get a line from source
for (char& ch : line)
if (is_whitespace(ch))
ch = ' ';
else if (!sensitive)
ch = tolower(ch);
buffer.str(line); //how does word become this value?
}
return *this;
}
显然,指针this
将是>>的结果,但我不明白该结果如何包括为单词分配字符串 istringstream buffer
.我只知道指针的基础知识,所以也许这是我的问题?
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
class Punct_stream {
public:
Punct_stream(istream& is)
: source{is}, sensitive{true} { }
void whitespace(const string& s) { white = s; }
void add_white(char c) { white += c; }
bool is_whitespace(char c);
void case_sensitive(bool b) { sensitive = b; }
bool is_case_sensitive() { return sensitive; }
Punct_stream& operator>>(string& s);
operator bool();
private:
istream& source;
istringstream buffer;
string white;
bool sensitive;
};
Punct_stream& Punct_stream::operator>>(string& s)
{
while (!(buffer>>s)) {
if (buffer.bad() || !source.good()) return *this;
buffer.clear();
string line;
getline(source,line); // get a line from source
for (char& ch : line)
if (is_whitespace(ch))
ch = ' ';
else if (!sensitive)
ch = tolower(ch);
buffer.str(line); //how does word become this value?
}
return *this;
}
Punct_stream::operator bool()
{
return !(source.fail() || source.bad()) && source.good(); }
bool Punct_stream::is_whitespace(char c) {
for (char w : white)
if (c==w) return true; return false;
}
int main ()
{
Punct_stream ps {cin};
ps.whitespace(";:,.?!()"{}<>/&$@#%^*|~");
ps.case_sensitive(false);
cout<<"Please input words."<<"n";
vector<string> vs;
for (string word; ps>>word;)// how does word get assigned a string? {
vs.push_back(word);
}
sort(vs.begin(), vs.end());
for (int i = 0; i<vs.size(); ++i) {
if (i==0 || vs[i]!=vs[i-1]) cout<<vs[i]<<"n";
}
}
诀
窍在于operator >>
内部的while
循环与从流中读取时通常执行的操作具有相反的逻辑。通常,你会做这样的事情(事实上,main
这样做):
while (stream >> aString)
但是请注意,提取器中的while
具有否定:
尝试从
buffer
中提取s
。如果失败,请执行循环的一次迭代,然后重试。
开始时,buffer
为空,因此提取s
将失败,并且将进入循环体。循环体的作用是从source
(被包装的流)中读取一行,将该行的选定字符转换为空格,并将此行设置为buffer
的内容 (通过buffer.str(line);
调用)。
因此,在转换线路后,它被排队进入buffer
。然后循环的下一次迭代来了,它再次尝试从buffer
中提取s
。如果该行有任何非空格,则将提取第一个单词(其余单词将保留在buffer
中以供进一步阅读)。如果该行只有空格,则再次输入循环主体。
成功提取s
后,循环终止,函数退出。
在下一次调用时,它将与buffer
中剩余的任何内容一起工作,根据需要从source
重新填充缓冲区(通过我上面解释的过程)。
相关文章:
- 在函数中返回无符号字符数组,但不返回指针
- 为什么需要返回指针来利用协方差?
- 野牛如何在解析树中返回指针
- 何时返回指针与返回对象的一般经验法则?
- 为什么使用 std::istream_iterator 构造函数返回指针
- 为什么我不能直接从返回指针的函数返回对指针的引用?
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 为什么下面的代码段返回指针指向的值而不是指针的地址?
- 使用 map<char,strring> 的迭代器返回指针 map<char,strring>*
- 如何从类成员函数返回指针,例如 size_t * class :: function(); 并使用类析构函数 ~size
- 函数返回动态强制转换的指针,返回指针,就好像它根本没有被转换一样
- 从函数返回对指针的引用与返回指针相同?
- C++ 为什么访问数组中的元素会返回指针?
- 关于函数返回指针
- 返回指针或对私有成员的引用的替代方法
- 从函数返回指针
- 通过从当地的C风格阵列中返回指针来获取一个悬空的指针
- 我应该如何定义返回指针的函数?(引用指针与指针指针)
- 为什么C 模板功能不支持返回指针
- 返回指针的语句 - 解释它是如何工作的以及为什么