【问题标题】:Which parts of the C standard library are not covered by (the rest of) the C++ standard library?C 标准库的哪些部分未被 C++ 标准库(其余部分)涵盖?
【发布时间】:2014-03-08 12:32:54
【问题描述】:

The C++ library includes the same definitions as the C language library

但 C++ 库似乎在非 C 库头文件中复制(/扩展)了 C 库的某些功能。比如C库有,C++库有; C 库有 ,C+ 库有

如果我需要一个字符串类,我认为我最好使用 而不是 ,因为 可以从 C++ 中的所有非 C 功能(例如异常)中受益。但是 C 库中的某些功能在 C++ 库中以任何其他形式都不存在。例如,我在 之外找不到像 memcpy 和 memcmp 这样的东西。

C 库的哪些部分在非 C 库标头中没有类似物?

(如果 C++ 标准的版本对此很重要,我对 C++11 感兴趣。)

【问题讨论】:

  • <string> 中没有类似 memcpy 和 memcmp 的原因是 <string> 不是 <cstring> 的重新实现,它是抽象问题空间“文本操作”的一种完全独立的方法”。恰好与 <cstring> 采用的方法有很大不同,部分原因是它使用了不同的语言,部分原因是与语言无关(例如 nul 终止与长度)。
  • memcpy=> std::copy, memcmp => std::equal

标签: c++ c++11 c++-standard-library


【解决方案1】:

标题并不多,所以我们只列出它们。有些可以用语言设施代替,而不是图书馆。 (我没有枚举每个标头中的每个函数,所以我可能错过了大多数同事都没有 C++ 替代方案的奇数函数。)

C library         C++ alternatives
assert.h          Exceptions
complex.h         <complex>
ctype.h           None (or maybe <locale>, if you want to jump down that rabbit-hole)
errno.h           None (but only applies to C functions)
fenv.h            None
float.h           <limits>
inttypes.h        (See breakdown)
   formatting     <iostream>
   strto...       <string> (C++11), <sstream>
   imaxabs        std::abs overloads
   imaxdiv        std::div overloads
iso646.h          Language
locale.h          <locale>
math.h            None (extended with C++ overloads)
setjmp.h          Exceptions
signal.h          None
stdarg.h          Variadic templates (C++11)
stdbool.h         Language
stddef.h          None
stdint.h          None
stdio.h           <iostream> etc.
stdlib.h          (See breakdown)
   atof etc.      <sstream>, <string> (C++11)
   rand etc.      <random> (C++11)
   malloc etc.    new, containers
   abort etc.     None
   bsearch etc.   <algorithm>
   abs etc.       None (extended with C++ overloads)
   mb strings     None
string.h          <string>, <algorithm>
tgmath.h          <cmath> (C++ overloads)
time.h            <chrono> (C++11)
wchar.h           <iostream> etc.
wctype.h          None

总结一下:

C 库的哪些部分在非 C 库标头中没有类似物?

[w]ctype.herrno.hfenv.hfenv.hmath.hsignal.hstddef.hstdint.hstdlib.h 的一部分。在 C++11 之前,还有 stdarg.htime.h 和更多 stdlib.h

【讨论】:

  • ctype.h 的特征被&lt;locale&gt; 中的构面std::ctype 覆盖。
  • stdint.h 中的功能被&lt;cstdint&gt; 覆盖
  • @Morwenn:好吧,我对知之甚少。
  • @timwoj:是的,这是 C 库头文件的 C++ 包装器,就像 的包装器一样
  • @MikeSeymour 难怪。我认为它是标准中“记录较少”的部分之一。与标准的其余部分相比,互联网上关于&lt;locale&gt; 的文章很少:)
猜你喜欢
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 2011-11-16
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多