逐行循环遍历char数组的最佳方式
Best way to loop through a char array line by line
我有一个char数组,其中包含从web检索到的文本文件。逐行循环并显示每个缓冲区的最佳方式是什么线
好吧,因为这是c++,所以将您的char数组转换为字符串流,然后像处理任何其他流/文件一样处理它。当然,这样做可以调用getline()之类的函数。
string tmpstr(chararry,length); // length optional, but needed if there may be zero's in your data
istringstream is(tmpstr);
string line;
while (getline(is,line)) {
// process line
}
另一个答案是一个很好的解决方案,但将char数组复制到临时字符串中会产生成本。这可以通过使用Boost的iostream
来避免——这可以避免复制缓冲区,同时让您将其视为任何其他流。
#include <boost/iostreams/stream.hpp>
boost::iostreams::stream<boost::iostreams::array_source> stream(buffer, size);
相关文章:
- 在c代码之间共享数据的最佳方式
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 在reactor中存储eventHandlers的最佳方式是什么
- 在AVX通道中混洗的最佳方式
- 从 T 创建 std::future 的最佳方式<T>
- C++:使用 std::unique_ptr 访问重载运算符++的最佳方式?
- 对列表列表中的元素进行分组的最佳方式
- 利用 GPU 的最佳方式
- 使用 QT C++过滤大数据的最佳方式
- 算法设计:用边界数字表示 2D 网格的最佳方式,以C++?
- 在C++中共享键值对的最佳方式
- 为Catch2中的外部文本文件指定路径的最佳方式
- 代表Quarto棋盘游戏棋子的最佳方式
- 等待线程的最佳方式是什么
- 将uint8_t*buffer和size_tbufferlen从C++传递到C中的API函数的最佳方式是什么
- 创建控制台菜单C++的最佳方式
- 只显示片段着色器的最佳方式是什么
- 复制文件的最佳方式是什么,以便我可以在复制过程中轻松取消复制?