Difference between lt;cstringgt; and lt;stringgt;(lt;cstringgt; 之间的区别和lt;字符串gt;)
问题描述
今天早些时候(实际上是昨天,由于我的时区),我在 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 风格字符串的函数——以空字符结尾的字符数组.这包括像 strlen 和 strcpy 这样的函数.它是 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.
这篇关于<cstring> 之间的区别和<字符串>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:<cstring> 之间的区别和<字符串>
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
