C++ 或 Boost 是否有像 C 的 memcmp 一样比较两个内存块的函数?
我试过谷歌,但我只得到了“memcmp”功能。
最佳答案
如果您想要一个可以处理指针和 STL 迭代器的函数,请查看 std::equal
在<algorithm>
.
我会考虑 std::equal
成为 C++ 的做事方式 std::memcmp
(它确实仍然是 C++,但 std::memcmp
不处理迭代器对象)。
#include <iostream>
#include <vector>
#include <algorithm>
int
main (int argc, char *argv[])
{
int a1[] = {1,2,3,4};
int a2[] = {1,9,3,5};
int * p1 = new int[4];
std::vector<int> vec (a2, a2+4);
*(p1++) = 1; *(p1++) = 2;
*(p1++) = 3; *(p1++) = 4;
p1 -= 4;
if (std::equal (a1, a1+4, p1)) {
std::cout << "memory of p1 == memory of a1\n";
}
if (std::equal (vec.begin (), vec.end (), p1) == false) {
std::cout << "memory of p1 != memory of vec\n";
}
}
输出
memory of p1 == memory of a1
memory of p1 != memory of vec
关于c++ - 是否有 C 的 "memcmp"的 native C++ 替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8485161/