为什么C++中有这么多字符串类型?
Why are so many string types in C++?
我喜欢 C,我有一本 C 书,叫做 Full 和 Full C 和 2 C++,我发现这些语言很棒,因为它们具有令人难以置信的功能和性能,但由于这些不同的类型,我不得不结束我的许多项目.
我说有std::string
,LPCSTR
,System::String
,TCHAR []
,char s [] = "ss"
,char * s
?
这主要在 GUI 应用程序中引起了极大的麻烦,WinAPI 存在 LPCSTR
与 char 或 std::string
不兼容的问题,现在在 CLR 应用程序中如果它具有 System::String
转换为 std::String
或 char * s
甚至 char s []
.
让人很头疼
为什么 C/C++ 的字符串类型不像 Java 中的 String
那样唯一?
每种字符串类型都有自己的目标,std::string
用于标准库,这是最常见的。
正如您所说,C++ 具有强大的功能和性能,并且这些字符串提供了更大的灵活性。 Char[]
和 char*
您可以使用更通用的字符串。
没有"many types of string in c++"。规范地有一个模板 std::basic_string
,它基本上是一个专门用于不同字符类型的字符串的容器。
std::string
是 std::basic_string<char>
上的一个方便类型定义。对于不同的底层字符类型,有更多这样的 typedef。
据我所知,标准 c 也只有一个官方认可的字符串标准。它是 ANSI 字符串,即 char
.
的空终止数组
您提到的所有其他内容要么与此等效(例如 LPCSTR
是指向常量字符串的长指针,即 const char*
),要么是库提供者编写的一些非标准扩展。
你的问题就像在问为什么有那么多GUI库一样。因为没有标准方法可以做到这一点,或者在某些方面缺乏标准方法,提供和支持自己的等效类型是设计决定。
底线是,在库级别或语言级别,这是不同权衡之间的设计决策。简单性、性能、字符支持等等等等。总的来说,storing text is hard.
嗯,首先我们必须回答这个问题:什么是字符串?
C 标准将其定义为由第一个空字符终止并包括第一个空字符的连续字符序列。1
它还提到使用 wchar_t
、char16_t
或 char32_t
而不是 char
.
的变体
它还提供了许多用于字符串操作的函数,以及用于符号方便的字符串文字。
因此,字符序列可以是一个字符串,char[]
可能包含一个字符串,而 char*
可能指向一个。
LPCSTR
是 const char*
的 windows typedef,添加的语义是它应该指向一个字符串或者是 NULL
.
TCHAR
是许多预处理器定义之一,用于将 windows 代码从 char
转换为 wchar_t
。根据 TCHAR
是什么,TCHAR[]
可能能够容纳字符串或宽字符串。
C++ 有点混淆,因为它添加了一种用于处理字符串的数据类型。为了减少歧义,string只用于抽象概念,你必须依靠上下文来消除歧义或更明确。
因此 C string 对应于 C++ null-terminated-byte-string 或 NTBS.2
是的,C++也知道它们种类繁多。
C++ 合并了 C 函数并添加了更多。
此外,C++ 有 std::basic_string<>
用于存储各种计数的字符串,以及一些方便的类型定义,如 std::string
.
现在我们学习第三种语言,即 C++/CLI。
它结合了我上面所说的所有 C++,并将 CLI 类型 System::String
添加到组合中。
System::String
是不可变的 UTF-16 计数字符串。
现在回答为什么C++没有定义一个单一的具体类型为字符串的问题可以回答:
为了互操作性、历史、效率和便利性,C++ 中有不同类型的字符串。始终使用正确的工具来完成工作。
Java 和 .Net 对字节数组、字符数组、字符串生成器等执行相同的操作。
参考1:C11定稿,字符串定义:
7. Library
7.1 Introduction
7.1.1 Definitions of terms
1 A string is a contiguous sequence of characters terminated by and including the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The length of a string is the number of bytes preceding the null character and the value of a string is the sequence of the values of the contained characters, in order.
参考文献 2:C++1z 草案 n4659 NTBS:
20.4.2.1.5.1 Byte strings [byte.strings]
1 A null-terminated byte string, or NTBS, is a character sequence whose highest-addressed element with defined content has the value zero (the terminating null character); no other element in the sequence has the value zero.163
2 The length of an NTBS is the number of elements that precede the terminating null character. An empty ntbs has a length of zero.
3 The value of an NTBS is the sequence of values of the elements up to and including the terminating null character.
4 A static NTBS is an NTBS with static storage duration.164
我喜欢 C,我有一本 C 书,叫做 Full 和 Full C 和 2 C++,我发现这些语言很棒,因为它们具有令人难以置信的功能和性能,但由于这些不同的类型,我不得不结束我的许多项目.
我说有std::string
,LPCSTR
,System::String
,TCHAR []
,char s [] = "ss"
,char * s
?
这主要在 GUI 应用程序中引起了极大的麻烦,WinAPI 存在 LPCSTR
与 char 或 std::string
不兼容的问题,现在在 CLR 应用程序中如果它具有 System::String
转换为 std::String
或 char * s
甚至 char s []
.
为什么 C/C++ 的字符串类型不像 Java 中的 String
那样唯一?
每种字符串类型都有自己的目标,std::string
用于标准库,这是最常见的。
正如您所说,C++ 具有强大的功能和性能,并且这些字符串提供了更大的灵活性。 Char[]
和 char*
您可以使用更通用的字符串。
没有"many types of string in c++"。规范地有一个模板 std::basic_string
,它基本上是一个专门用于不同字符类型的字符串的容器。
std::string
是 std::basic_string<char>
上的一个方便类型定义。对于不同的底层字符类型,有更多这样的 typedef。
据我所知,标准 c 也只有一个官方认可的字符串标准。它是 ANSI 字符串,即 char
.
您提到的所有其他内容要么与此等效(例如 LPCSTR
是指向常量字符串的长指针,即 const char*
),要么是库提供者编写的一些非标准扩展。
你的问题就像在问为什么有那么多GUI库一样。因为没有标准方法可以做到这一点,或者在某些方面缺乏标准方法,提供和支持自己的等效类型是设计决定。
底线是,在库级别或语言级别,这是不同权衡之间的设计决策。简单性、性能、字符支持等等等等。总的来说,storing text is hard.
嗯,首先我们必须回答这个问题:什么是字符串?
C 标准将其定义为由第一个空字符终止并包括第一个空字符的连续字符序列。1
它还提到使用 wchar_t
、char16_t
或 char32_t
而不是 char
.
的变体
它还提供了许多用于字符串操作的函数,以及用于符号方便的字符串文字。
因此,字符序列可以是一个字符串,char[]
可能包含一个字符串,而 char*
可能指向一个。
LPCSTR
是 const char*
的 windows typedef,添加的语义是它应该指向一个字符串或者是 NULL
.
TCHAR
是许多预处理器定义之一,用于将 windows 代码从 char
转换为 wchar_t
。根据 TCHAR
是什么,TCHAR[]
可能能够容纳字符串或宽字符串。
C++ 有点混淆,因为它添加了一种用于处理字符串的数据类型。为了减少歧义,string只用于抽象概念,你必须依靠上下文来消除歧义或更明确。
因此 C string 对应于 C++ null-terminated-byte-string 或 NTBS.2
是的,C++也知道它们种类繁多。
C++ 合并了 C 函数并添加了更多。
此外,C++ 有 std::basic_string<>
用于存储各种计数的字符串,以及一些方便的类型定义,如 std::string
.
现在我们学习第三种语言,即 C++/CLI。
它结合了我上面所说的所有 C++,并将 CLI 类型 System::String
添加到组合中。
System::String
是不可变的 UTF-16 计数字符串。
现在回答为什么C++没有定义一个单一的具体类型为字符串的问题可以回答:
为了互操作性、历史、效率和便利性,C++ 中有不同类型的字符串。始终使用正确的工具来完成工作。
Java 和 .Net 对字节数组、字符数组、字符串生成器等执行相同的操作。
参考1:C11定稿,字符串定义:
7. Library
7.1 Introduction
7.1.1 Definitions of terms
1 A string is a contiguous sequence of characters terminated by and including the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The length of a string is the number of bytes preceding the null character and the value of a string is the sequence of the values of the contained characters, in order.
参考文献 2:C++1z 草案 n4659 NTBS:
20.4.2.1.5.1 Byte strings
[byte.strings]
1 A null-terminated byte string, or NTBS, is a character sequence whose highest-addressed element with defined content has the value zero (the terminating null character); no other element in the sequence has the value zero.163
2 The length of an NTBS is the number of elements that precede the terminating null character. An empty ntbs has a length of zero.
3 The value of an NTBS is the sequence of values of the elements up to and including the terminating null character.
4 A static NTBS is an NTBS with static storage duration.164