Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::strcmp

      From cppreference.com
      <cpp‎ |string‎ |byte
       
       
       
       
      Defined in header<cstring>
      int strcmp(constchar* lhs,constchar* rhs);

      Compares two null-terminated byte strings lexicographically.

      The sign of the result is the sign of the difference between the values of the first pair of characters (both interpreted asunsignedchar) that differ in the strings being compared.

      The behavior is undefined iflhs orrhs are not pointers to null-terminated strings.

      Contents

      [edit]Parameters

      lhs, rhs - pointers to the null-terminated byte strings to compare

      [edit]Return value

      Negative value iflhs appears beforerhs in lexicographical order.

      Zero iflhs andrhs compare equal.

      Positive value iflhs appears afterrhs in lexicographical order.

      [edit]Example

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

      Output:

      GarfieldHeathcliffHobbesSnagglepuss

      [edit]See also

      compares a certain number of characters from two strings
      (function)[edit]
      compares two wide strings
      (function)[edit]
      compares two buffers
      (function)[edit]
      compares two strings in accordance to the current locale
      (function)[edit]
      C documentation forstrcmp
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/byte/strcmp&oldid=160645"

      [8]ページ先頭

      ©2009-2025 Movatter.jp