int strncmp( const char* lhs, const char* rhs, std::size_t count ); Compares at most count characters of two possibly null-terminated arrays. The comparison is done lexicographically. Characters following the null character are not compared. The sign of the result is the sign of the dif...
// CPP program to illustratestrncmp()#include<cstring>#include<iostream>voiddisplay(char* abc,char* xyz,intres,intcount){if(res >0)std::cout<< xyz <<" come-before "<< abc;elseif(res <0)std::cout<< abc <<" come-before "<< xyz;elsestd::cout<<"First "<< count <<" characters...
int strncmp( const char* lhs, const char* rhs, std::size_t count ); 比较二个可能空终止的数组的至多 count 个字符。按字典序进行比较。不比较后随空字符的字符。 结果的符号是被比较的数组中首对字符(都转译成 unsigned char )的值间的差的符号。 若出现越过 lhs 或rhs 结尾的访问,则行为未定义。
std::strncmp 定义于头文件<cstring> intstrncmp(constchar*lhs,constchar*rhs,std::size_tcount); 比较二个可能空终止的数组的至多count个字符。按字典序进行比较。不比较后随空字符的字符。 结果的符号是被比较的数组中首对字符(都转译成unsignedchar)的值间的差的符号。
#include <cstring> #include <iostream> void demo(const char* lhs, const char* rhs, int sz) { int rc = std::strncmp(lhs, rhs, sz); if(rc == 0) std::cout << "First " << sz << " chars of [" << lhs << "] equal [" << rhs << "]\n"; else if(rc < 0) std::...