std::strxfrm
STD:strxfrm
Defined in header | | |
---|---|---|
std::size_t strxfrm( char* dest, const char* src, std::size_t count | | |
转换以空结尾的字节字符串。src
转换为实现定义的形式,以便将两个转换后的字符串与std::strcmp
提供的结果与将原始字符串与std::strcoll
,在当前的C语言环境中。
第一count
将转换字符串的字符写入目标,包括终止空字符,并返回完全转换字符串的长度,不包括终止空字符。
如果dest
数组不够大。如果dest
和src
重叠。
如果count
是0
,然后dest
允许为空指针。
注记
可以接收整个转换字符串的缓冲区的正确长度是1+std::strxfrm(nullptr, src, 0)
...
此函数用于使用相同的字符串或字符串集进行多个依赖于区域设置的比较,因为使用它更有效。std::strxfrm
将所有字符串只转换一次,然后将转换后的字符串与std::strcmp
...
参数
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 |
返回值
转换后的字符串的长度,不包括终止空字符.
例
二次
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <cassert>
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';
else
std::cout << in2 << " before " << in1 << '\n';
std::cout << "In lexicographical comparison: ";
if(in1 < in2)
std::cout << in1 << " before " << in2 << '\n';
else
std::cout << in2 << " before " << in1 << '\n';
}
二次
产出:
二次
In the Czech locale: hrnec before chrt
In lexicographical comparison: chrt before hrnec
二次
另见
wcsxfrm | transform a wide string so that wcscmp would produce the same result as wcscoll (function) |
---|---|
do_transform virtual | transforms a string so that collation can be replaced by comparison (virtual protected member function of std::collate) |
strcoll | compares two strings in accordance to the current locale (function) |
c strxfrm文档
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。