-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#21 MODERN C++ FOR C PROGRAMMERS-PART-1 #70
#21 MODERN C++ FOR C PROGRAMMERS-PART-1 #70
Conversation
【认领校对】 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
相关意见需要fix,另外注意下标点符号和盘古之白
欢迎来到为 C 程序员准备的现代 C++ 的第一部分,请阅读对这个系列文章的目标和内容的介绍。 | ||
|
||
In this part we start with C++ features that you can use to spice up your code ‘line by line’, without immediately having to use all 1400 pages of ‘The C++ Programming Language’. | ||
在这个部分我们从某些 C++ 特性开始介绍,这样我们就可以逐行的应用在我们的代码上,而不必立即使用 ’The C++ Programming Language‘ 书籍的全部1400行。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
而不必立即使用 ’The C++ Programming Language‘ 书籍的全部1400行。
-->
而不必等到读完厚厚的1400多行的《The C++ Programming Language》一书后,再去使用C++的全部特性
这里要注意的是 【而不必立即【使用】 ’The C++ Programming Language‘ 【书籍】【的】全部1400【行】】
使用书籍的行,如果这样提炼一下这句话,就能发现问题了,我们不是使用【行】,而是使用c++
|
||
|
||
The relation goes further - the entire C library is included in C++ ‘by reference’, and C++ knows how to call all C code. And conversely, it is entirely possible to call C++ functions from C. | ||
当整个 C 语言的库都”通过引用“被包含在 C++ 库中时,这种关系就变得更加紧密。因此 C++ 知道怎样调用所有的 C 代码。反过来,在 C 程序中调用 C++ 函数也是完全有可能的。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
引号方向不对
>In other words, no feature should be added to C++ which would make any existing code (not using the new feature) larger or slower, nor should any feature be added for which the compiler would generate code that is not as good as a programmer would create without using the feature. | ||
>零开销原则是 C++ 设计的指导原则。它指出:你没使用的,你就不必承担它的开销(无论是时间上还是空间上),进一步讲:对于你使用了的功能,你无法手写出更好的代码。 | ||
> | ||
>换句话说,任何添加到 C++ 中的特性都不会让任何已有的代码(不使用新特性的代码)变长或变慢,如果编译器产生的代码比程序员不使用这些特性手写的代码性能差的话,就不应该添加这项特性。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
变长
应该是占用更多的空间吧
这值得研究一会。这里的 `cmp()` 函数是为 `qsort()` 函数准备的,同时也定义了排序的顺序。 | ||
|
||
Main is main as in C, but then we see the first oddity: auto. We’ll cover this later, but auto almost always does what you think it does: calculate the required type and use it. | ||
首先是 C 语言中的 `main` 函数,其中我们看到了第一个奇怪的地方:auto。我们会在稍后详细介绍这一点,但 auto 的作用和我们想象的差不多:首先计算需要使用的类型,然后使用这个类型。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main is main as in C : 我理解这里Main和mian的含义是不同的。
主函数和C语言一样,都main函数。
这里要区分【as】和【是】的区别
在现代操作系统中,上面的结构体占 24 个字节。`capacity` 字段通常用来记录已分配了多少内存,因此`mystring`结构体知道什么时候需要重新分配内存。不必每添加一个字符就重新分配一次内存,这是一个巨大的胜利。 | ||
|
||
Frequently however, the things we store in strings are a lot shorter than 24 bytes. For this reason, modern C++ std::string implementations implement Small String Optimization, which allows them to store 16 or even 21 bytes of characters within their own storage, without using `malloc()`, which is a speedup. | ||
然而,我们经常要在字符串中存储的内容要比 24 字节小很多。因此,现代C++中 `std::string` 的实现了对小字符串优化,允许它们在自己的存储空间中存 16 甚至 21 个字节的字符串,而不必使用 `malloc()` 函数,这样提高了运行速度。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
现代C++中 std::string
的实现了对小字符串优化 :不通顺
防止不必要的 `malloc()` 函数调用的另一个好处是,字符串数组会存储在连续的内存中,这对于内存缓冲机制非常重要,这通常是提高运行速度的全部原因。 | ||
|
||
After years of design, std::string may not be everything to everyone, but consistent with the ‘zero overhead’ principle, it beats what you would have quickly written by hand. | ||
经过多年的设计之后,`std::string` 可能还不能让所有人都满意,但是因为坚持了’零开销‘原则,所以这要优于你在短时间内手写出的 `string` 类。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
引号有问题
ok |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
666
(提交翻译请使用下面的模板,其他类型pr请删除)
closes #21
1.译者信息
(转载时的署名信息,有需要请补充)
2.疑问区
(可在此区域列举有疑问的语句,提示校对人员重点关注)
3.自查表
4.发布信息(译者无需填写)
发布链接:
@hanxiaomax