Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::strxfrm

      From cppreference.com
      <cpp‎ |string‎ |byte
       
       
       
       
      Defined in header<cstring>
      std::size_t strxfrm(char* dest,constchar* src,std::size_t count);

      Transforms the null-terminated byte string pointed to bysrc into the implementation-defined form such that comparing two transformed strings withstd::strcmp gives the same result as comparing the original strings withstd::strcoll, in the current C locale.

      The firstcount characters of the transformed string are written to destination, including the terminating null character, and the length of the full transformed string is returned, excluding the terminating null character.

      The behavior is undefined if thedest array is not large enough. The behavior is undefined ifdest andsrc overlap.

      Ifcount is0, thendest is allowed to be a null pointer.

      Contents

      [edit]Notes

      The correct length of the buffer that can receive the entire transformed string is1+ std::strxfrm(nullptr, src,0).

      This function is used when making multiple locale-dependent comparisons using the same string or set of strings, because it is more efficient to usestd::strxfrm to transform all the strings just once, and subsequently compare the transformed strings withstd::strcmp.

      [edit]Parameters

      dest - pointer to the first element of the array where the transformed string will be written
      src - pointer to the first character of a null-terminated byte string to transform
      count - maximum number of characters to be written

      [edit]Return value

      The length of the transformed string, not including the terminating null-character.

      [edit]Example

      Run this code
      #include <cassert>#include <cstring>#include <iomanip>#include <iostream>#include <string> int main(){char* loc=std::setlocale(LC_COLLATE,"cs_CZ.iso88592");assert(loc); std::string in1="hrnec";std::string out1(1+ std::strxfrm(nullptr, in1.c_str(),0),' ');std::string in2="chrt";std::string out2(1+ std::strxfrm(nullptr, in2.c_str(),0),' ');     std::strxfrm(&out1[0], in1.c_str(), out1.size());    std::strxfrm(&out2[0], in2.c_str(), out2.size()); std::cout<<"In the Czech locale: ";if(out1< out2)std::cout<< in1<<" before "<< in2<<'\n';elsestd::cout<< in2<<" before "<< in1<<'\n'; std::cout<<"In lexicographical comparison: ";if(in1< in2)std::cout<< in1<<" before "<< in2<<'\n';elsestd::cout<< in2<<" before "<< in1<<'\n';}

      Possible output:

      In the Czech locale: hrnec before chrtIn lexicographical comparison: chrt before hrnec

      [edit]See also

      transform a wide string so thatwcscmp would produce the same result aswcscoll
      (function)[edit]
      [virtual]
      transforms a string so that collation can be replaced by comparison
      (virtual protected member function ofstd::collate<CharT>)[edit]
      compares two strings in accordance to the current locale
      (function)[edit]
      C documentation forstrxfrm
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/byte/strxfrm&oldid=152868"

      [8]ページ先頭

      ©2009-2025 Movatter.jp