不等长比较字符串:string.compare
愿型 int compare(size_type __pos1, size_type __n1, const basic_string& __str,
size_type __pos2, size_type __n2)
unsigned char buf0[5] = {0xff, 0xfd, 1, 2, 3};
char *buf1 = new char[5];
memcpy(buf1, buf0, 5);
char buf2[] = {1, 2, 3};
std::string str2 = reinterpret_cast<const char *>(buf2);
int ret = str2.compare(0, 3, buf1, 2, 3); // 意思是取str2的0到3(不含下标3)也就是 1 2 3 与 buf1下标从2开始的3个升度对比
std::cout << ret << std::endl; / / 结果 ret = 0, 比较的结果相等。
compare 其它用法如下:
比较自己和str,
int compare( const basic_string &str );
int compare( const char *str );
比较自己的子串和str,子串以index索引开始,长度为length
int compare( size_type index, size_type length, const basic_string &str );
比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
int compare( size_type index, size_type length, const basic_string &str, size_type index2, size_type length2 );
比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length
int compare( size_type index, size_type length, const char *str, size_type length2 );