C++ <string> 和 <string.h> 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9257665/
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 <string> and <string.h>?
提问by Valmond
How come this code
这个代码怎么来的
std::map <std::string , int> m;
m["a"]=1;
compiles with (I'm using MSVC 2010)
编译(我使用的是 MSVC 2010)
#include <string>
but not with
但不是与
#include <string.h>
?
?
回答by Ajay
<string.h>
contains old functions likestrcpy
,strlen
for C style null-terminated strings.<string>
primarily contains thestd::string
,std::wstring
and other classes.
<string.h>
包含旧函数,如strcpy
,strlen
用于 C 风格的空终止字符串。<string>
主要包含std::string
,std::wstring
和其他类。
回答by Prasoon Saurav
string.h
is a C header not a C++ header, period!
string.h
是 C 头文件而不是 C++ 头文件,句号!
回答by matiu
<string.h>
is cstring - http://www.cplusplus.com/reference/clibrary/cstring/
<string.h>
是 cstring - http://www.cplusplus.com/reference/clibrary/cstring/
<string>
is the c++ string class - http://www.cplusplus.com/reference/string/
<string>
是 C++ 字符串类 - http://www.cplusplus.com/reference/string/
Edit per Nicol Bolas comment below and a bit of googling:
根据下面的 Nicol Bolas 评论和一些谷歌搜索进行编辑:
<cstring>
will usually import the same things as <string.h>
but into the std
namespace.
<string.h>
will usually import everything into the global namespace.
It appears to depend on the library implementation you're using though according to my googling.
<cstring>
通常会将与<string.h>
but相同的东西导入std
命名空间。
<string.h>
通常会将所有内容导入全局命名空间。根据我的谷歌搜索,它似乎取决于您正在使用的库实现。
Personally I only ever use <cstring>
if I need C style string helpers.
我个人只<cstring>
在需要 C 样式字符串助手时才使用。
回答by Abhijeet Rastogi
string.h
is C's header file while string
is C++'s header file.
string.h
是C的头文件,string
而是C++的头文件。
回答by Nicol Bolas
<string.h>
contains C-library string functions. strlen
, strcmp
, etc.
<string.h>
包含 C 库字符串函数。strlen
,strcmp
等等。
<string>
contains the definition for std::basic_string
, which has the typedefs std::string
and std::wstring
. That's the difference.
<string>
包含 for 的定义std::basic_string
,它具有 typedefsstd::string
和std::wstring
. 这就是区别。
They really have no relationship at all, outside of the fact that they both deal with strings.
除了它们都处理字符串这一事实之外,它们真的没有任何关系。
回答by Sanish
They are entirely different headers.
它们是完全不同的标题。
<string>
is C++ string
class
<string>
是 C++string
类
<string.h> or <cstring>
defines functions to manipulate C strings and arrays
<string.h> or <cstring>
定义操作 C 字符串和数组的函数
回答by jpstrube
I believe <string.h>
is just used for C and <string>
for C++. So including string.h
wont work.
我相信<string.h>
仅用于 C 和<string>
C++。所以包括string.h
不会工作。
回答by Zeta
As stated, string.h
and cstring
are C headers (while cstring
is basically a C++ wrapper for string.h
), containing functions for C strings, which are char[]
terminated by '\0'
. You want to use the c++ class string, which header is <string>
.
如上所述,string.h
并且cstring
是 C 头文件(虽然cstring
基本上是 的 C++ 包装器string.h
),包含 C 字符串的函数,这些函数char[]
以'\0'
. 您想使用 C++ 类字符串,其标头是<string>
.
回答by PankajSingh
<string.h>
is a C standard library header while <string>
is a cpp in fact all the c standard header files have .h
extension an non of cpp have .h
.
<string.h>
是一个 C 标准库头文件,而<string>
实际上是一个 cpp,所有的 c 标准头文件都有.h
扩展名,非 cpp 有.h
。
回答by James Liu
string.h is for c compatible c++ string class string is for pure c++ string class
string.h 适用于与 C 兼容的 C++ 字符串类 string 适用于纯 C++ 字符串类