名前空間
変種
操作

std::strcmp

提供: cppreference.com
< cpp‎ | string‎ | byte
ヘッダ <cstring> で定義
int strcmp( const char *lhs, const char *rhs );

2つのヌル終端バイト文字列を辞書的に比較します。

結果の符号は比較する文字列内の最初の異なる文字の組の値 (どちらも unsigned char として解釈されます) の差の符号です。

lhs または rhs がヌル終端文字列を指すポインタでない場合、動作は未定義です。

目次

[編集] 引数

lhs, rhs - 比較するヌル終端バイト文字列を指すポインタ

[編集] 戻り値

辞書順で lhsrhs より前に現れる場合は負の値。

lhsrhs が等しい場合はゼロ。

辞書順で lhsrhs より後に現れる場合は正の値。

[編集]

#include <vector>
#include <cstring>
#include <algorithm>
#include <iostream>
 
int main() 
{
    std::vector<const char*> cats {"Heathcliff", "Snagglepuss", "Hobbes", "Garfield"};
    std::sort(cats.begin(), cats.end(), [](const char *strA, const char *strB) {
        return std::strcmp(strA, strB) < 0;
    }); 
 
    for (const char *cat : cats) {
        std::cout << cat << '\n';
    }
}

出力:

Garfield
Heathcliff
Hobbes
Snagglepuss

[編集] 関連項目

2つの文字列の文字を一定量比較します
(関数) [edit]
2つのワイド文字列を比較します
(関数) [edit]
2つのバッファを比較します
(関数) [edit]
現在のロケールに従って2つの文字列を比較します
(関数) [edit]