最終更新日時:
が更新

履歴 編集

function
<cstring>

std::memcmp

namespace std {
  int memcmp(const void* s1, const void* s2, size_t n);
}

概要

メモリデータを比較する。

効果

s1が指すオブジェクトとs2が指すオブジェクトの先頭nバイトを比較する。各バイトはunsigned charとして比較される。

戻り値

2つのオブジェクトで最初に異なるバイトについて、s1側のバイトがs2側のバイトより大きい場合は正の整数、小さい場合は負の整数を返す。先頭nバイトがすべて等しい場合は0を返す。

備考

  • この関数は、フリースタンディング処理系でも使用できる。

#include <cstring>
#include <iostream>

int main()
{
  const unsigned char a[] = {0x01, 0x02, 0x03};
  const unsigned char b[] = {0x01, 0x02, 0x04};

  int r = std::memcmp(a, b, 3);

  std::cout << (r < 0 ? "less" : r > 0 ? "greater" : "equal") << std::endl;
}

出力

less

バージョン

言語

  • C++98

関連項目

  • strcmp: 文字列を比較する
  • strncmp: 文字列を比較する(上限サイズ指定)

参照