字符串比较。如何比较字符串与std :: wstring? WRT strcmp [英] String comparisons. How can you compare string with std::wstring? WRT strcmp

查看:885
本文介绍了字符串比较。如何比较字符串与std :: wstring? WRT strcmp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两种格式,我预期会有点兼容,因为他们都是字符串。我试图执行strcmp与一个字符串和std :: wstring,并且我确信C ++的古茹知道,这将只是不编译。是否可以比较这两种类型?

I am trying to compare two formats that I expected would be somewhat compatible, since they are both generally strings. I have tried to perform strcmp with a string and std::wstring, and as I'm sure C++ gurus know, this will simply not compile. Is it possible to compare these two types? Is there an easy conversion here?

推荐答案

您需要转换 char * string - ISO C语言中的multibyte - ISO C语言中的 wchar_t * string - wide character。执行此操作的标准函数称为 mbstowcs (多字节字符串到宽字符串)

You need to convert your char* string - "multibyte" in ISO C parlance - to a wchar_t* string - "wide character" in ISO C parlance. The standard function that does that is called mbstowcs ("Multi-Byte String To Wide Character String")

注意在注释中,这是一个C99函数,因此不是ISO C ++一致,但可能支持C ++实现作为扩展。 MSVC和g ++都支持它。

NOTE: as Steve pointed out in comments, this is a C99 function and thus is not ISO C++ conformant, but may be supported by C++ implementations as an extension. MSVC and g++ both support it.

因此:

const char* input = ...;

std::size_t output_size = std::mbstowcs(NULL, input, 0); // get length
std::vector<wchar_t> output_buffer(output_size);

// output_size is guaranteed to be >0 because of \0 at end
std::mbstowcs(&output_buffer[0], input, output_size);

std::wstring output(&output_buffer[0]);

一旦你有两个 wstring 比较像往常一样。注意,这将使用当前系统语言环境进行转换(即在Windows上这将是当前的ANSI代码页) - 通常这只是你想要的,但偶尔你需要处理一个特定的编码,在这种情况下上述操作不会执行,您需要使用 iconv

Once you have two wstrings, just compare as usual. Note that this will use the current system locale for conversion (i.e. on Windows this will be the current "ANSI" codepage) - normally this is just what you want, but occasionally you'll need to deal with a specific encoding, in which case the above won't do, and you'll need to use something like iconv.

所有其他答案似乎都用于直接代码点翻译(即相当于(wchar_t)c 对于字符串中的每个 char c )。这可能不适用于所有区域设置,但它将工作,如果例如。您的 char 都是ASCII或Latin-1,而您的 wchar_t 是Unicode。如果你确定这是你真正想要的,最快的方法是完全避免转换,并使用 std :: lexicographical_compare

All other answers seem to go for direct codepoint translation (i.e. the equivalent of (wchar_t)c for every char c in the string). This may not work for all locales, but it will work if e.g. your char are all ASCII or Latin-1, and your wchar_t are Unicode. If you're sure that's what you really want, the fastest way is actually to avoid conversion altogether, and to use std::lexicographical_compare:

#include <algorithm>

const char* s = ...;
std::wstring ws = ...;

const char* s_end = s + strlen(s);

bool is_ws_less_than_s = std::lexicographical_compare(ws.begin, ws.end(),
                                                      s, s_end());
bool is_s_less_than_ws = std::lexicographical_compare(s, s_end(),
                                                      ws.begin(), ws.end());
bool is_s_equal_to_ws = !is_ws_less_than_s && !is_s_less_than_ws;

如果您特别需要测试是否相等,请使用 std :: equal 和一个长度检查:

If you specifically need to test for equality, use std::equal with a length check:

#include <algorithm>

const char* s = ...;
std::wstring ws = ...;

std::size_t s_len = strlen(s);
bool are_equal =
    ws.length() == s_len &&
    std::equal(ws.begin(), ws.end(), s);

这篇关于字符串比较。如何比较字符串与std :: wstring? WRT strcmp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆