c++ - Visual C++、wchar_t* 命令行参数无法比较?

标签 c++ visual-c++ if-statement comparison wchar-t

我在尝试基于哪个字符串 argv[1] 等于某个赋值时遇到了一些麻烦。

int _tmain(int argc, _TCHAR* argv[]) //wchar_t
{   
    if (argc != 2)
        exit(1);

    if (argv[1] == L"-foo")
        printf("Success!\n");

    wprintf(argv[1]);
    printf("\n");

    system("pause");
    return 0;
}

如果我使用参数“-foo”运行可执行文件,我会收到以下输出:

-foo

应该是:

Success!
-foo

字符串正是我想要的,但 if 条件仍然为假。 wchar_t 字符串是否不能使用 == 运算符进行比较?如果是这样,我该如何正确比较它们?

最佳答案

Preliminary Note: Unicode and Unicode character in this answer, given the context of the question itself, refers to the UCS-2 (up to XP) and UTF-16 (starting with XP) encodings, used interchangeably with wide character, wchar_t, WCHAR and other terms in the context of the Win32 API. The Unicode standards offer multiple encodings such as UTF-8, UTF-16 and UTF-32 to encode the same number of characters - different incarnations of the standard have a different scope. Surrogate code points are used to escape from the Basic Multilingual Plane (BMP), roughly the first 64K code points, and thus encode more than could be encoded with 16bit characters and one character per code-point. The surrogate extensions were developed for the Unicode 2.0 standard, which was passed in the year NT 4.0 was released, but some years after the first "Unicode-capable" version of Windows, NT 3.51, got released. That original standard didn't account for more characters than the BMP and that is why Unicode character or wide character are even now used synonymous with Unicode in the Win32 API context, although this is inaccurate.

回答您提出的基本问题:

Are wchar_t strings simply not comparable using the "==" operator?

不,它们不是,“ANSI”字符串也不是,即使用 char类型作为基础。请记住,C 字符串(同时基于 wchar_tchar)是一个指针。这意味着 ==您正在比较两个绝对不相等的指针值。毕竟,一个是文字字符串(即在您的程序镜像中),而另一个分配在堆上的某个位置。所以他们绝对是两个不同的实体。

如果您想使用 ==您将不得不使用 C++ 等语言和 STL 类 std::string (或 std::basic_string<_TCHAR> )或(在 Windows 上)ATL 类 CString (或者更确切地说 CStringT )。这些类有时称为智能字符串类,并使用覆盖 operator==() 的 C++ 工具。 .但是,您应该记住,语义因实现而异,因此并非每个智能字符串类都会比较字符串内容。有些人可能只是比较 this 的相等性(即是否是同一个实例),而其他人可能会自行决定比较字符串内容是否区分大小写。

要比较 C 字符串,您可以使用以下适用于您的用例的函数:

  • 对于“ANSI”字符 ( char ) 字符串:strcmp , _stricmp (以及“计数”变体:_strncmp_strnicmp……还有更多)
  • 对于 Unicode 字符 ( wchar_t ) 字符串:wcscmp , _wcsicmp (以及“计数”变体:_wcsncmp_wcsnicmp……还有更多)
  • 对于变量字符“type”(TCHAR)字符串:_tcscmp , _tcsicmp (以及“计数”变体:_tcsncmp_tcsnicmp……还有更多)

你可以记住这些前缀:

  • str -> 字符串
  • wcs -> 宽字符串
  • tcs -> T 字符串

旁注:与 #include <tchar.h>windows.hTEXT_T是等效的,用于声明一个字符串文字,该文字将是“ANSI”或 Unicode,具体取决于构建时的定义。 _TCHAR也是如此和 TCHAR显然,而后者似乎在 Win32 API 上下文中受到青睐。

因此 Unicode 构建将扩展 _T("something")L"something" ,而“ANSI”构建会将其扩展为 "something" .

至于 TCHAR,请考虑阅读以下提出的论点:Is TCHAR still relevant? (由 rubenvb 指出)有支持和反对 TCHAR 的有效观点/_TCHAR使用,您应该做出决定并坚持下去 - 即保持一致

关于c++ - Visual C++、wchar_t* 命令行参数无法比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437394/

相关文章:

c++ - 使用 vector 和类

visual-c++ - 静态链接Casablanca/CPPREST SDK

php - 如果设置了一个日期而没有设置另一个日期

java - hbase 扫描仪 .next() 返回 null

python - 除以0时,我希望它打印给定的文本

c++ - 为什么这个函数没有导致错误?

c++ - 接口(interface)/抽象类应该只包含纯虚方法吗?

c++ - 忽略 GCC "error: braces around scalar initializer for type"错误。让他们警告

c++ - 启动服务器时出现 std::exception (WinSock2)

mysql - 我如何构建 libmySQL?