C++ STL低级编程
是否可以使用 STL 进行一些低级编程,至少是容器和算法?我需要做一个简单的操作系统,并且必须知道尝试 C++ 和 STL 或使用纯 C 是否可行。任何资源都可以接受。谢谢。
编辑---------
Boost 怎么样?
Is it possible to do some low level programming with STL, at least containers and algorithms? I need to do a simple OS and have to know if is feasible to try the C++ and STL or go with plain C. Any resources are accepted.Thanks.
Edit---------
And how about Boost?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我熟悉的 C++ STL 的大多数实现都严重依赖于操作系统(特别是内存分配)。我不会排除在操作系统内核中使用 C++ STL,但是您必须做大量工作才能将 STL 的实现移植到您的环境中(例如,实现内存分配器、进行异常处理)工作,编写 iostreams 兼容性填充程序以输出到屏幕/TTY/其他),并让编译器正确定位您的实现。总体来说,用 C 语言编写操作系统可能会减少工作量。:)
Most implementations of the C++ STL I'm familiar with have been significantly dependent on an OS (particularly for memory allocation). I'm not going to rule out using the C++ STL in an operating system kernel, but you'd have to do a lot of work to port an implementation of the STL to your environment (e.g, implement a memory allocator, make exception handling work, write an iostreams compatibility shim to output to the screen/TTY/whatever), and to get a compiler to target your implementation correctly. It may be less work overall to just write the OS in C. :)
您应该完全使用 C++ STL。即使您只使用裸指针和数组,STL 算法也会极大地阐明您的思维和编程。 Alexander Stepanov 的 STL 的天才之处在于,算法是根据迭代器来表达的,而迭代器是指针的泛化,因此您可以将他的任何算法应用于原始指针和数组,并且运行时间为零。
另外,这是最好的部分。如果您像 Alexander Stepanov 一样思考,并使用他的“指向开头的指针”和“指向过去的结尾的指针”习惯用法,则所有加一 (+ 1) 和减一 (- 1) 项都将从您的数组中删除和循环逻辑,您将永远不必再注意差一逻辑错误。
您应该编写一个操作系统,该操作系统本身将字符串表示为一对字节*,以开始和结束 UTF8 内存块。然后你的操作系统可以替换 Unices 并将我们从空终止的有符号 char* 字符串中解放出来。
You should totally use the C++ STL. Even if you only work with bare pointers and arrays, the STL Algorithms will vastly clarify your thinking and programming. The genius of Alexander Stepanov's STL is that Algorithms are expressed with respect to Iterators, and Iterators are a generalization of pointers, so you can apply any of his algorithms to raw pointers and arrays with zero run-time overheard.
Plus, here's the best part. If you think like Alexander Stepanov, and use his pointer-to-beginning and pointer-to-past-the-end idiom, all of the plus one (+ 1) and minus one (- 1) terms will drop out of your array and loop logic and you'll never again have to watch out for off-by-one logic errors.
You should write an OS that natively represents strings as a pair of byte* to begin and past-the-end of a UTF8 memory block. Then your OS can replace the Unices and liberate us from null-terminated signed char* strings.
您可以(并且应该)将 C++ 用于操作系统。取决于操作系统中的位置
代码发生时,您可能必须放弃部分(或全部)标准
库,在某些情况下甚至可能是一些功能
(例外?);这一切都取决于具体情况。
对于 STL 容器,它们肯定可以用于
某些级别,前提是您指定自定义分配器。 (图书馆
operator new
的实现将取决于操作系统功能,因此操作系统本身可能不可用。虽然连这个
取决于操作系统的设计。)并且应该没有问题
大多数算法。
You can (and should) use C++ for an OS. Depending on where in the OS
the code occurs, you might have to forgo parts (or all) of the standard
library, and in some cases perhaps, even some functionality
(exceptions?); it all depends on the context.
With regards to the STL containers, they would certainly be usable at
some levels, provided you specify custom allocators. (The library
implementation of
operator new
will depend on OS functionality, soprobably won't be available in the OS itself. Although even this
depends on the design of the OS.) And there should be no problem with
most of the algorithms.