我不能使用strstr,memchr因为数组可以包含任意数量的\ 0字符,是否有任何有效的方法可以做到这一点?我必须找到所有位置(索引)或指针.
C++中的一块蛋糕:
#include <string>
const std::string needle = get_byte_sequence();
const std::string haystack = get_data();
std::size_t pos = haystack.find(needle);
// found if pos != std::string::npos
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用通用算法:
#include <algorithm>
std::string::const_iterator it = std::search(data.begin(), data.end(), needle.begin(), needle.end());
if (it != data.end())
{
// needle found at position std::distance(data.begin(), it);
}
else
{
// needle not found
}
Run Code Online (Sandbox Code Playgroud)
请记住,C++ string对象可以包含任意数据.要从字节缓冲区创建字符串,请将其大小传递给构造函数:
char buf[200]; // fill with your data; possibly full of zeros!
std::string s(buf, 200); // no problem
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2076 次 |
| 最近记录: |