string.h中和CString的区别? [英] Difference between string.h and cstring?

查看:152
本文介绍了string.h中和CString的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是文件string.h 的CString 之间的区别?

What is the difference between string.h and cstring?

哪一个应该被用于 C 键,这对于 C ++ 一(如果有的话)?

Which one should be used for C and which one for C++ (if at all)?

推荐答案

在C ++中你应该包括的CString 作为标题,而在C你应该包括 string.h中作为标题。

In C++ you should include cstring as the header while in c you should include string.h as the header.

在C ++

#include<cstring>

在C

#include<string.h>

在C ++标准库还提供了

C标准库的特点及作为一般的命名惯例,他们是pre-挂起由 C 来相应的名称在C标准库。

Features of C standard Library are also provided in the C++ Standard library and as a general naming convention they are pre-pended by an c to the corresponding names in C standard library.

例如:结果
文件string.h 变成的CString 结果
stdio.h中变成 cstdio 等等...

For Example:
string.h becomes cstring
stdio.h becomes cstdio and so on...

由于其他答案已经添加不同的维度来这个讨论,我觉得不得不提及圣标清部份位。

Since other answers have added different dimensions to this discussion,I felt compelled to refer the holy standard to clear ths bit.

C ++ 11 20.9.14.6&安培; 7

表55描述了头&LT; CString的方式&gt; 结果
  内容是一样的标准C库头,在21.7指定的变化了memchr()

Table 55 describes the header <cstring>.
The contents are the same as the Standard C library header , with the change to memchr() specified in 21.7.

21.7空值终止序列实用工具规定:

函数签名了memchr(常量无效*,INT,为size_t)由两个声明来代替:

The function signature memchr(const void*, int, size_t) shall be replaced by the two declarations:

常量无效*了memchr(常量无效* S,INT C,为size_t N); 结果
void *的了memchr(无效* S,INT C,为size_t N);

应具有相同的行为正本报关。

both of which shall have the same behavior as the original declaration.

附录D(标准)兼容特性[DEPR] 规定:

D.6 C标准库头

1为了与C标准库和中联code TR的兼容性,C ++标准库提供了25 C头文件,如表151。

1 For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, as shown in Table 151.

,其中包括:

&LT;&ASSERT.H GT; &LT;&float.h中GT; &LT;&math.h中GT; &LT;&STDDEF.H GT; &LT; tgmath.h&GT;
  &LT; complex.h&GT; &LT; inttypes.h&GT; &LT;&SETJMP.H GT; &LT;&stdio.h中GT; &LT;&time.h中GT;
  &LT;&文件ctype.h GT; &LT; iso646.h&GT; &LT;&signal.h中GT; &LT; stdint.h&GT; &LT; uchar.h&GT;
  &LT;&errno.h中GT; &LT;&limits.h中GT; &LT;&STDARG.H GT; &LT;&stdlib.h中GT; &LT; wchar.h&GT;
  &LT; fenv.h&GT; &LT;&locale.h文件GT; &LT; stdbool.h&GT; &LT;&string.h中GT; &LT; wctype.h&GT;

此外上,

2 每一个C头,每个具有形式 name.h 的名称,行为就好像被放置在标准库空间每一个名字相应的 CNAME头放在全局命名空间范围之内。这是不确定的这些名字是否首先声明或命名空间的命名空间范围(3.3.6)中定义性病,然后注入到明确使用-声明的全局命名空间范围(7.3.3)。

2 Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).

3 [示例:头文件&LT; cstdlib&GT; 确实提供了空间std内的声明和定义。它也可以在全球命名空间内提供这些名称。头文件&LT;&stdlib.h中GT; 确实提供了相同的声明和定义全局命名空间内,就像在C标准。它也可以提供空间std内这些名称。例如末端]

3 [ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]

从上面的引用:结果
我站在我前面的建议纠正,似乎有超过文件string.h 使用没有明显的优势的CString 而为@Alf建议有可能是由于使用的CString 作为头时使用不合格的函数名的一些编译问题。因此,考虑的帽子有使用没有明显的DIS-优势文件string.h 或优势使用的CString ,我想无论是如果以适当的方式可以用在C ++中使用。

Conclusion:

From the above references:
I stand corrected on my earlier suggestion, there seems to be no apparent advantage of using cstring over string.h while as @Alf suggested there might be some compilation issues due to use of unqualified function names when using cstring as header. So given hat there is no apparent dis-advantage of using string.h or advantage of using cstring, I think either can be used in C++ if used in a proper manner.

这篇关于string.h中和CString的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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