名前空間
変種
操作

std::wcsncmp

提供: cppreference.com
< cpp‎ | string‎ | wide
ヘッダ <cwchar> で定義
int wcsncmp( const wchar_t* lhs, const wchar_t* rhs, std::size_t count );

2つのヌル終端ワイド文字列の最大 count 個のワイド文字を比較します。 比較は辞書順で行われます。

結果の符号は比較する文字列内の最初の異なるワイド文字の組の値の差の符号です。

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

目次

[編集] 引数

lhs, rhs - 比較するヌル終端ワイド文字列を指すポインタ
count - 比較する最大文字数

[編集] 戻り値

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

lhsrhs が等しい場合はゼロ。

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

[編集]

#include <iostream>
#include <cwchar>
#include <clocale>
#include <locale>
 
void demo(const wchar_t* lhs, const wchar_t* rhs, int sz)
{
    int rc = std::wcsncmp(lhs, rhs, sz);
    if(rc == 0)
        std::wcout << "First " << sz << " characters of ["
                  << lhs << "] equal [" << rhs << "]\n";
    else if(rc < 0)
        std::wcout << "First " << sz << " characters of ["
                  << lhs << "] precede [" << rhs << "]\n";
    else if(rc > 0)
        std::wcout << "First " << sz << " characters of ["
                  << lhs << "] follow [" << rhs << "]\n";
}
 
int main()
{
    const wchar_t str1[] = L"안녕하세요";
    const wchar_t str2[] = L"안녕히 ���십시오";
 
    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout.imbue(std::locale("en_US.utf8"));
    demo(str1, str2, 5);
    demo(str2, str1, 8);
    demo(str1, str2, 2);
}

出力:

First 5 characters of [안녕하세요] precede [안녕히 가십시오]
First 8 characters of [안녕히 가십시오] follow [안녕하세요]
First 2 characters of [안녕하세요] equal [안녕히 가십시오]

[編集] 関連項目

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