< cstring>和< string> [英] Difference between <cstring> and <string>

查看:68
本文介绍了< cstring>和< string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



简而言之,当我使用

时遇到了几个编译错误 1

  #include< cstring> 

这是由框架代码提供的问题之一,然后转到

  #include< string> 

所有编译错误在神奇地消失。



然而,在提交到采访街,我不得不加上 c



这是我第一次被非标准化的东西....



我的问题是:< string> < cstring> 半小时?






1
$ b

Visual Studio 2012的一个错误,如果使用< cstring> 是:



< >

错误C2338:C ++标准不为此类型提供散列。



$ b




c:\program files(x86)\microsoft visual studio 11.0 \vc\include\xstddef


可能 string unordered_map



如果使用< string> ,g ++会产生一个错误:


'strlen'未在此范围内声明



解决方案>

cstring 头提供了处理C风格字符串 - 以null结尾的字符数组的函数。这包括 strlen strcpy 的函数。它是C的经典 string.h 头文件的C ++版本。



string 头提供 std :: string 类及相关函数和运算符。



头部具有相似的名称,但它们之间不是真正的相关。它们涵盖单独的任务。


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++).

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.

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

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


1 For anyone who is curious:

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

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

in

c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef

possibly for string as key in unordered_map

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

'strlen' was not declared in this scope

解决方案

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.

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.

这篇关于&lt; cstring&gt;和&lt; string&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆