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>
?
吗?
8 个解决方案
#1
56
-
<string.h>
contains old functions likestrcpy
,strlen
for C style null-terminated strings. - <字符串。h> 包含像strcpy, strlen这样的旧函数,用于C样式的空终止字符串。
-
<string>
primarily contains thestd::string
,std::wstring
and other classes. -
主要包含std::string、std::wstring和其他类。
#2
19
string.h
is a C header not a C++ header, period!
字符串。h是一个C头,不是一个c++头,句号!
#3
15
<string.h>
is cstring - http://www.cplusplus.com/reference/clibrary/cstring/
<字符串。h> 是cstring - http://www.cplusplus.com/reference/clibrary/cstring/。
<string>
is the c++ string class - 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.
Personally I only ever use <cstring>
if I need C style string helpers.
我个人只使用
#4
10
string.h
is C's header file while string
is C++'s header file.
字符串。h是C的头文件,而字符串是c++的头文件。
#5
8
<string.h>
contains C-library string functions. strlen
, strcmp
, etc.
<字符串。h> 包含c库字符串函数。函数比较字符串等。
<string>
contains the definition for std::basic_string
, which has the typedefs std::string
and std::wstring
. That's the difference.
They really have no relationship at all, outside of the fact that they both deal with strings.
他们根本没有关系,除了他们都处理字符串。
#6
5
They are entirely different headers.
它们是完全不同的标题。
<string>
is C++ string
class
<string.h> or <cstring>
defines functions to manipulate C strings and arrays
<字符串。h> 或
#7
2
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>
.
如上所述,字符串。h和cstring是C头(而cstring基本上是字符串的c++包装器),包含了C字符串的函数,这些函数是由“\0”终止的。您想要使用c++类字符串,它的头是
#8
2
I believe <string.h>
is just used for C and <string>
for C++. So including string.h
wont work.
我认为 <字符串。h> 只是用于C和
#1
56
-
<string.h>
contains old functions likestrcpy
,strlen
for C style null-terminated strings. - <字符串。h> 包含像strcpy, strlen这样的旧函数,用于C样式的空终止字符串。
-
<string>
primarily contains thestd::string
,std::wstring
and other classes. -
主要包含std::string、std::wstring和其他类。
#2
19
string.h
is a C header not a C++ header, period!
字符串。h是一个C头,不是一个c++头,句号!
#3
15
<string.h>
is cstring - http://www.cplusplus.com/reference/clibrary/cstring/
<字符串。h> 是cstring - http://www.cplusplus.com/reference/clibrary/cstring/。
<string>
is the c++ string class - 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.
Personally I only ever use <cstring>
if I need C style string helpers.
我个人只使用
#4
10
string.h
is C's header file while string
is C++'s header file.
字符串。h是C的头文件,而字符串是c++的头文件。
#5
8
<string.h>
contains C-library string functions. strlen
, strcmp
, etc.
<字符串。h> 包含c库字符串函数。函数比较字符串等。
<string>
contains the definition for std::basic_string
, which has the typedefs std::string
and std::wstring
. That's the difference.
They really have no relationship at all, outside of the fact that they both deal with strings.
他们根本没有关系,除了他们都处理字符串。
#6
5
They are entirely different headers.
它们是完全不同的标题。
<string>
is C++ string
class
<string.h> or <cstring>
defines functions to manipulate C strings and arrays
<字符串。h> 或
#7
2
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>
.
如上所述,字符串。h和cstring是C头(而cstring基本上是字符串的c++包装器),包含了C字符串的函数,这些函数是由“\0”终止的。您想要使用c++类字符串,它的头是
#8
2
I believe <string.h>
is just used for C and <string>
for C++. So including string.h
wont work.
我认为 <字符串。h> 只是用于C和