简体   繁体   English

boost :: asio :: buffer如何遍历字节

[英]boost::asio::buffer how to iterate over the bytes

I am trying to implement a function that takes a boost::const_buffer and iterates over its bytes in a loop. 我正在尝试实现一个使用boost :: const_buffer并在循环中遍历其字节的函数。 I saw that there is a buffers iterator, but it seems to be applicable for boost::asio::buffers_type. 我看到有一个缓冲区迭代器,但它似乎适用于boost :: asio :: buffers_type。 I didn't find any examples for simple traversal of a buffer. 我没有找到简单遍历缓冲区的任何示例。

So, is it the standard way to access the buffer via buffer_cast into a native type such as char* and then traverse it by traditional methods? 那么,这是通过buffer_cast访问本地类型(例如char *)然后通过传统方法遍历它的标准方法吗? Or is there any direct helper function to do this? 还是有直接的辅助功能可以做到这一点?

boost::asio::buffer_cast<>

#include <boost/asio.hpp>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>

namespace asio = boost::asio;

void test(asio::const_buffer const& buffer)
{
    auto first = asio::buffer_cast<const char*>(buffer);
    auto last = first + asio::buffer_size(buffer);

    std::copy(first, last, std::ostream_iterator<char>(std::cout));
    std::cout << std::endl;
}

int main()
{
    std::string s = "hello";

    test(asio::buffer(s));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM