std::strncmp
STD:斯特恩西姆
Defined in header | | |
---|---|---|
int strncmp( const char* lhs, const char* rhs, size_t count | | |
最多比较count
两个以空结尾的字节字符串的字符。比较是按字典顺序进行的。
结果的符号是第一对字符%28的值之间差值的符号,这两个字符都被解释为unsigned char
%29,在所比较的字符串中存在差异。
如果lhs
或rhs
不是指向以空结尾的字符串的指针。
不对空字符后面的字符进行比较。
参数
lhs, rhs | - | pointers to the null-terminated byte strings to compare |
---|---|---|
count | - | maximum number of characters to compare |
返回值
负值lhs
出现在前面rhs
按字典顺序排列。
零中频lhs
和rhs
比较平等。
正值lhs
出现在rhs
按字典顺序排列。
例
二次
#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::cout << "First " << sz << " chars of ["
<< lhs << "] precede [" << rhs << "]\n";
else if(rc > 0)
std::cout << "First " << sz << " chars of ["
<< lhs << "] follow [" << rhs << "]\n";
}
int main()
{
demo("Hello, world!", "Hello, everybody!", 13
demo("Hello, everybody!", "Hello, world!", 13
demo("Hello, everybody!", "Hello, world!", 7
demo("Hello, everybody!" + 12, "Hello, somebody!" + 11, 5
}
二次
产出:
二次
First 13 chars of [Hello, world!] follow [Hello, everybody!]
First 13 chars of [Hello, everybody!] precede [Hello, world!]
First 7 chars of [Hello, everybody!] equal [Hello, world!]
First 5 chars of [body!] equal [body!]
二次
另见
strcmp | compares two strings (function) |
---|---|
wcsncmp | compares a certain amount of characters from two wide strings (function) |
memcmp | compares two buffers (function) |
strcoll | compares two strings in accordance to the current locale (function) |
c strncmp文档
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。