为什么不能用C ++重新实现C标准函数用C ++元素/风格? [英] Why doesn't C++ reimplement C standard functions with C++ elements/style?

查看:116
本文介绍了为什么不能用C ++重新实现C标准函数用C ++元素/风格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个具体的例子,考虑的atoi(常量标准::字符串&放大器;)。这是非常令人沮丧,因为我们作为程序员需要这么多的使用它。


  1. 更​​普遍的问题是,为什么没有C ++标准库重新实现与C ++字符串标准C库,C ++载体或其它C ++标准的元素,而不是preserve旧的C标准库和迫使我们使用旧的的char * 接口?


    它的耗时和code来翻译这两个接口之间的数据类型是不容易被优雅。


  2. 它是兼容的原因,考虑到还有更多的传统的C code比这些天,preserving这些C标准的接口会从C code转换到C ++容易得多?


  3. 可用于C ++
  4. 另外,我听到很多其他图书馆做了很多增强和扩展STL.So的确实有库支持这些功能呢?


PS:考虑更多的答案,第一个具体问题,我编辑了很多澄清问题概述,我更好奇地问问题


解决方案

  

另外一个更普遍的问题是,为什么不STL reimplementate所有标准C库


由于旧的C库做的伎俩。 C ++标准库只现有功能重新工具,如果他们能做到这一点比老版更好显著。而对于C库的某些部分,书写新的C ++的好处 - 实现只是没有大到足以证明额外的标准化工作。

至于的atoi 之类的,还有的在C ++标准库的这些新版本,在的std :: stringstream的类。

要从一个类型T转换为U型:

  T的;
你出来;
性病:: stringstream的SSTR(中);
SSTR>>出;

由于与iostream库的其余部分,它不是完美的,它是pretty冗长,它的IM pressively速度慢等问题,但它的作品,通常是的够好。它可以处理各种规模的整数,浮点值,C和C ++字符串,它定义了运营商的LT任何其他物体;<


  

编辑:另外,我听到很多其他图书馆avaliable对C ++做了很多增强和扩展STL.So的也有库支持这些功能。


升压有一个的boost :: lexical_cast的它包装的std :: stringstream的。与功能,你可以写上如下:

 你出来=的boost :: lexical_cast的< U>(中);

For a specific example, consider atoi(const std::string &). This is very frustrating, since we as programmers would need to use it so much.

  1. More general question is why does not C++ standard library reimplement the standard C libraries with C++ string,C++ vector or other C++ standard element rather than to preserve the old C standard libraries and force us use the old char * interface?

    Its time consuming and the code to translate data types between these two interfaces is not easy to be elegant.

  2. Is it for compatible reason,considering there was much more legacy C code than these days and preserving these C standard interfaces would make translation from C code to C++ much easier?

  3. In addition,I have heard many other libraries available for C++ make a lot of enhancement and extensions to STL.So does there libraries support these functions?

PS: Considering much more answers to the first specific question, I edit a lot to clarify the question to outline the questions that I am much more curious to ask.

解决方案

Another more general question is why do not STL reimplementate all the standard C libraries

Because the old C libraries do the trick. The C++ standard library only re-implements existing functionality if they can do it significantly better than the old version. And for some parts of the C library, the benefit of writing new C++-implementations just isn't big enough to justify the extra standardization work.

As for atoi and the like, there are new versions of these in the C++ standard library, in the std::stringstream class.

To convert from a type T to a type U:

T in;
U out;
std::stringstream sstr(in);
sstr >> out;

As with the rest of the IOStream library, it's not perfect, it's pretty verbose, it's impressively slow and so on, but it works, and usually it is good enough. It can handle integers of all sizes, floating-point values, C and C++ strings and any other object which defines the operator <<.

EDIT:In addition,I have heard many other libraries avaliable for C++ make a lot of enhancement and extensions to STL.So does there libraries support these functions?

Boost has a boost::lexical_cast which wraps std::stringstream. With that function, you can write the above as:

U out = boost::lexical_cast<U>(in);

这篇关于为什么不能用C ++重新实现C标准函数用C ++元素/风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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