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
Difference between <cstring> and <string>
提问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 c
back; 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 string
as 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 cstring
header provides functions for dealing with C-style strings — null-terminated arrays of characters. This includes functions like strlen
and strcpy
. It's the C++ version of the classic string.h
header from C.
所述cstring
头部提供的功能与C语言风格的处理字符串-字符空终止阵列。这包括像strlen
和这样的函数strcpy
。它是来自 C 的经典string.h
头文件的 C++ 版本。
The string
header provides the std::string
class 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 C
headers have the same base name, except for a leading c
and 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::string
and 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>