<cstring> 之间的区别和<字符串>

2022-01-23 00:00:00 visual-studio g++ c++

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

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

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

To be brief, I came across several compilation errors1 when I was using

#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.

但是,在提交到 Interview Street 后,我??不得不将 c 添加回来;否则会出现编译错误.

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

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

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

我的问题是:<string><cstring> 里面的什么花了我(宝贵的)半个多小时?

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

1 对于任何好奇的人:

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

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

错误 C2338:C++ 标准不提供此类型的哈希.

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

c:program files (x86)microsoft visual studio 11.0vcincludexstddef

c:program files (x86)microsoft visual studio 11.0vcincludexstddef

可能将 string 作为 unordered_map

如果 使用 <string> 则 g++ 的一个错误是:

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

'strlen' 未在此范围内声明

'strlen' was not declared in this scope

推荐答案

cstring 标头提供了处理 C 风格字符串的函数――以空字符结尾的字符数组.这包括像 strlenstrcpy 这样的函数.它是 C 中经典 string.h 标头的 C++ 版本.

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.

string 标头提供std::string 类和相关函数和运算符.

The string header provides the std::string class and related functions and operators.

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

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

相关文章