C++ <cstring> 和 <string> 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12824595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:39:22  来源:igfitidea点击:

Difference between <cstring> and <string>

c++visual-studiog++

提问by Dante May Code

Earlier today (actually yesterday due to my time-zone) I was attempting a programming interview using Visual Studio 2012for C++ on Interview Street (which uses g++).

今天早些时候(实际上是昨天,由于我的时区),我在Interview Street(使用g++)尝试使用Visual Studio 2012for C++ 进行编程面试。

To be brief, I came across several compilation errors1when I was using

简而言之,我在使用时遇到了几个编译错误1

#include <cstring>

which was provided by the skeleton code in one of the question, and after turning to

这是由问题之一中的骨架代码提供的,然后转向

#include <string>

all compilation errors magically disappeared.

所有编译错误都神奇地消失了。

However, upon submission to Interview Street, I had to add cback; otherwise I got compilation errors.

然而,在提交给Interview Street后,我不得不加c回来;否则我会遇到编译错误。

It was the first time I was bitten by non-standardization....

这是我第一次被非标准化所咬......

My question is: what inside <string>and <cstring>took me (precious) more than half an hour?

我的问题是:什么里面<string><cstring>我花了(珍贵)超过半小时?



1For anyone who is curious:

1对于任何好奇的人:

One error by Visual Studio 2012 if using <cstring>is:

Visual Studio 2012 的一个错误using <cstring>是:

error C2338: The C++ Standard doesn't provide a hash for this type.

错误 C2338:C++ 标准不提供此类型的散列。

in

c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef

c:\程序文件 (x86)\Microsoft Visual Studio 11.0\vc\include\xstddef

possibly for stringas key in unordered_map

可能string因为关键unordered_map

One error by g++ if using <string>is:

g++ 的一个错误using <string>是:

'strlen' was not declared in this scope

'strlen' 未在此范围内声明

采纳答案by Rob Kennedy

The cstringheader provides functions for dealing with C-style strings — null-terminated arrays of characters. This includes functions like strlenand strcpy. It's the C++ version of the classic string.hheader from C.

所述cstring头部提供的功能与C语言风格的处理字符串-字符空终止阵列。这包括像strlen和这样的函数strcpy。它是来自 C 的经典string.h头文件的 C++ 版本。

The stringheader provides the std::stringclass and related functions and operators.

所述string头提供的std::string类和相关功能和操作符。

The headers have similar names, but they're not really related beyond that. They cover separate tasks.

标题具有相似的名称,但除此之外它们并没有真正相关。它们涵盖不同的任务。

回答by juanchopanza

<cstring>has the C string code from the C header string.h. C++has a convention where Cheaders have the same base name, except for a leading cand no trailing .h. All the contents are available under the std::namespace.

<cstring>具有来自 C 头文件 string.h 的 C 字符串代码。C++有一个约定,其中C标头具有相同的基本名称,除了前导c和没有尾随.h. 所有内容都在std::命名空间下可用。

<string>has the standard library std::stringand related functions

<string>有标准库std::string及相关功能

回答by Rudolf Mühlbauer

In C++, you wouldn't use #include <somefile.h>, but instead #include <somefile>. Now C++ has its string classes in <string>, but the c-string functions are also available, which would be in <string.h>. C++ uses for 'traditional' c- include files. Therefore, <cstring>and <string>

在 C++ 中,您不会使用#include <somefile.h>, 而是#include <somefile>. 现在 C++<string><string.h>. C++ 用于“传统”c-包含文件。因此,<cstring><string>

http://www.cplusplus.com/reference/clibrary/cstring/

http://www.cplusplus.com/reference/clibrary/cstring/