gpt4 book ai didi

围绕 2 个字符串流 ("dialogue"模仿的 C++ 包装器)

转载 作者:行者123 更新时间:2023-11-30 02:55:38 25 4
gpt4 key购买 nike

我目前正在为两个流编写一个包装器。最后,我想模仿一段对话Person使我的代码清晰易用。

声明

class Person
{
public:
void process();

private:
std::stringstream _request;
std::stringstream _response;
}

用法

Person daniel;
std::stringstream answer;
daniel << "Hello" << std::endl; // f.e std::endl means end of the phrase
daniel << "How" << "are you";
daniel << "doing?";
daniel >> answer;
daniel.process();
std::cout << "Daniel says: " << answer;
// or
std::cout << "Daniel says: " << daniel;

感觉可以重载<<来实现和 >>运营商,但我被困住了。我从未将流用于这些目的。我在谷歌上搜索了很多,显然仍然无法理解基本的东西。

简而言之

std::ostream& operator<<(std::ostream& o, const person& p)
{
output << p._response.rdbuf();

return (output);
}

std::istream& operator>>(std::istream& i, person& p)
{
p._request << i.rdbuf();
// internal process() call probably will be better
return (p._request);
}

有些例子确实很棒,但我会非常高兴有粗略的想法让我走出僵局。

最佳答案

老实说,我不明白你为什么要这个,但我想这是某种练习或家庭作业。无论如何,实现此功能有一些微妙之处,我怀疑您自己无法理解,所以这里是可行的解决方案。

#include <iostream>
#include <sstream>

class Person {
public:
void process() {
// Let's see what we've got in `_request`
std::cout << _request.rdbuf() << std::endl;

// Do some processing to produce correponding _response

// In this example we hardcode the response
_response << "I'm fine, thanks!";
}

private:
std::stringstream _request;
std::stringstream _response;

// Make them all friends so that they can access `_request` and `_response`
friend Person& operator<<(Person& p, std::string const& s);
friend Person& operator<<(Person& p, std::ostream& (*f)(std::ostream&));
friend std::ostream& operator<<(std::ostream &o, Person& p);
friend Person& operator>>(Person& p, std::string& s);
};

Person& operator<<(Person& p, const std::string& s) {
p._request << s;

return p;
}

// Notice this peculiar signature, it is required to support `std::endl`
Person& operator<<(Person& p, std::ostream& (*f)(std::ostream&)) {
p._request << f;

return p;
}

// Somewhat conventional stream operator overload (in terms of signature)
std::ostream& operator<<(std::ostream &o, Person& p) {
o << p._response.rdbuf();

return o;
}

Person& operator>>(Person& p, std::string& s) {
// NOTE: This will read not the whole `_reponse` to `s`, but will stop on
// the first whitespace. This is the default behavior of `>>` operator of
// `std::stringstream`.
p._response >> s;

return p;
}

值得一提的是,就您要实现的功能而言,您的第一次尝试是完全错误的。这归结为这样一个事实,即您似乎在遵循有关流运算符重载的传统教程,而这里的方法应该有所不同才能实现所需的功能。尤其要注意提议的流运算符重载的签名。

此外,您必须添加更多重载以支持其他输入类型,例如 int。本质上,您必须添加更多类似于 std::basic_ostream 的重载。 提供开箱即用的功能。特别注意最后一条:

basic_ostream& operator<<(basic_ostream& st, 
std::basic_ostream& (*func)(std::basic_ostream&));

这家伙在那里支持std::endl。请注意,我已经为您向 Person 添加了类似的重载,因此 std::endl 将正常工作(见下文)。其他重载,例如原始类型,留给您作为练习。

阅读回应

您一开始想要的那个。

int main() {
Person daniel;

daniel << "Hello" << std::endl;
daniel << "How " << "are you";
daniel << " doing?" << std::endl;

// We are ready to process the request so do it
daniel.process();

// And Daniel's answer is...
std::cout << "Daniel says: " << daniel << std::endl;

return 0;
}

阅读回应:另一种方式

这个有点笨拙和繁琐,基本上是 Person& operator>>(Person& p, std::string& s) 重载实现中注释的结果(见上)。

int main() {
Person daniel;

daniel << "Hello" << std::endl;
daniel << "How " << "are you";
daniel << " doing?" << std::endl;

// We are ready to process the request so do it
daniel.process();

// And Daniel's answer is...
std::string part1;
std::string part2;
std::string part3;

// Will read "I'm"
daniel >> part1;

// Will read "fine,"
daniel >> part2;

// Will read "thanks!"
daniel >> part3;

std::cout << "Daniel says: " << part1 << " " << part2 << " " << part3 << std::endl;

return 0;
}

关于围绕 2 个字符串流 ("dialogue"模仿的 C++ 包装器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16247146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com