std::strncmp
提供: cppreference.com
| ヘッダ <cstring> で定義
|
||
| int strncmp( const char* lhs, const char* rhs, size_t count ); |
||
2つのヌル終端バイト文字列を最大 count 文字比較します。 比較は辞書的に行われます。
結果の符号は比較する文字列内の最初の異なる文字の組の値 (どちらも unsigned char として解釈されます) の差の符号です。
lhs または rhs がヌル終端文字列を指すポインタでない場合、動作は未定義です。
ヌル文字より後の文字は比較されません。
目次 |
[編集] 引数
| lhs, rhs | - | 比較するヌル終端バイト文字列を指すポインタ |
| count | - | 比較する最大文字数 |
[編集] 戻り値
辞書順で lhs が rhs より前に現れる場合は負の値。
lhs と rhs が等しい場合はゼロ。
辞書順で lhs が rhs より後に現れる場合は正の値。
[編集] 例
Run this code
#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!]
[編集] 関連項目
| 2つの文字列を比較します (関数) | |
| 2つのワイド文字列の文字を一定量比較します (関数) | |
| 2つのバッファを比較します (関数) | |
| 現在のロケールに従って2つの文字列を比較します (関数) | |
| strncmp の C言語リファレンス
| |

