【问题标题】:Boost read_until docs提升 read_until 文档
【发布时间】:2024-04-23 21:05:02
【问题描述】:

总的来说,我几乎是 Boost 的新手,更具体地说,我是 Boost Asio 的新手——我有点陷入了困境。我正在阅读 Boost 文档,我想知道我是否在示例中遇到了拼写错误。在 this page 的示例下,有以下内容:

std::string data;
std::string n = boost::asio::read_until(s,
    boost::asio::dynamic_buffer(data), '\n');
std::string line = data.substr(0, n);
data.erase(0, n);

我无法编译它,我认为这是因为 n 应该被声明为 std::size_t,而不是 std::string——我在这方面是正确的,还是我误解了?

【问题讨论】:

  • The Reference for read_until is pretty clear,因为 read_until 仅从其调用返回 size_t
  • 是的,你是对的。 std::string::substr 在第二个参数中也需要 std::size_t
  • Xirema,我在引用参考页。

标签: c++ boost c-strings stdstring


【解决方案1】:

是的,这是文档中的错字。

示例如下:

std::string data;
std::size_t n = boost::asio::read_until(s,
    boost::asio::dynamic_buffer(data), '\n');
std::string line = data.substr(0, n);
data.erase(0, n);

感谢 Xirema 和 S.M 的确认。

【讨论】: