
C++
文章平均质量分 70
面向对象思考
这个作者很懒,什么都没留下…
展开
-
01-简单字符串题目
信息竞赛中的字符串处理,实际开发中应该也可以借鉴原创 2022-11-13 21:05:25 · 449 阅读 · 0 评论 -
最简单的C++程序
C++程序基本构造定义和使用变量原创 2022-10-30 20:41:28 · 252 阅读 · 0 评论 -
01 准备开发环境
介绍安装VS2022 C++开发环境的过程。原创 2022-10-23 15:04:37 · 297 阅读 · 0 评论 -
C++核心准则讨论:如果一个类是一个容器,请为其提供一个initializer-list构造函数
Discussion: If a class is a container, give it an initializer-list constructor讨论:如果一个类是一个容器,请为其提供一个initializer-list构造函数Reason(原因)It is common to need an initial set of elements.通常需要一组初始元素。Example(示例)template<typename T> class Vecto..翻译 2021-05-09 20:34:44 · 486 阅读 · 0 评论 -
C++核心准则讨论:如果一个类是资源句柄,则它需要一个构造函数,一个析构函数以及复制和/或移动操作
Discussion: If a class is a resource handle, it needs a constructor, a destructor, and copy and/or move operations讨论:如果一个类是资源句柄,则它需要一个构造函数,一个析构函数以及复制和/或移动操作Reason(原因)To provide complete control of the lifetime of the resource. To provide a coheren.翻译 2021-05-05 19:56:07 · 498 阅读 · 1 评论 -
C++核心准则讨论:按值返回容器(依靠移动或复制省略高效率)
Discussion: Return containers by value (relying on move or copy elision for efficiency)讨论:按值返回容器(依靠移动或复制省略高效率)Reason(原因)To simplify code and eliminate a need for explicit memory management. To bring an object into a surrounding scope, thereby exte.翻译 2021-04-28 19:51:02 · 556 阅读 · 0 评论 -
C++核心准则讨论:使用模板来表达容器(和其他资源句柄)
Discussion: Use templates to express containers (and other resource handles)讨论:使用模板来表达容器(和其他资源句柄)Reason(原因)To provide statically type-safe manipulation of elements.提供元素的静态类型安全操作。Example(示例)template<typename T> class Vector { //..翻译 2021-04-25 20:08:01 · 419 阅读 · 0 评论 -
C++核心准则讨论:切勿让指针的生命周期超出其指向的对象
Discussion: Never let a pointer outlive the object it points to讨论:切勿让指针的生命周期超出其指向的对象Reason(原因)To avoid extremely hard-to-find errors. Dereferencing such a pointer is undefined behavior and could lead to violations of the type system.避免极难发现的错误。防..翻译 2021-04-24 20:35:36 · 534 阅读 · 1 评论 -
C++核心准则 讨论:“原始”指针或引用绝对不是资源句柄
Discussion: A "raw" pointer or reference is never a resource handle讨论:“原始”指针或引用绝对不是资源句柄Reason(原因)To be able to distinguish owners from views.为了能够从各种表现中区分所有者。Note(注意)This is independent of how you "spell"pointer : T*, T&, Ptr<T>...翻译 2021-04-22 18:49:59 · 399 阅读 · 0 评论 -
C++核心准则讨论:持有没有被句柄管理的资源时切勿抛出异常
Discussion: Never throw while holding a resource not owned by a handle讨论:持有没有被句柄管理的资源时切勿抛出异常Reason(原因)That would be a leak.这回引发资源泄露。Example(注意)void f(int i){ FILE* f = fopen("a file", "r"); ifstream is { "another file" }; //..翻译 2021-04-20 19:21:18 · 432 阅读 · 0 评论 -
C++核心准则讨论:提供强大的资源安全性;也就是说,永远不要泄漏任何您认为是资源的东西
Discussion: Provide strong resource safety; that is, never leak anything that you think of as a resource讨论:提供强大的资源安全性;也就是说,永远不要泄漏任何您认为是资源的东西Reason(原因)Prevent leaks. Leaks can lead to performance degradation, mysterious error, system crashes, and s.翻译 2021-04-17 19:29:49 · 413 阅读 · 0 评论 -
C++核心准则讨论:保持定义复制,移动和析构函数的一致性
Discussion: Define Copy, move, and destroy consistently讨论:保持定义复制,移动和析构函数的一致性Reason(原因)???Note(注意)If you define a copy constructor, you must also define a copy assignment operator.如果定义了拷贝构造函数,则还必须定义一个拷贝赋值运算符。Note(注意)If you define a ...翻译 2021-04-14 20:15:18 · 575 阅读 · 0 评论 -
C++核心准则讨论:析构,释放和交换操作必须永不失败
Discussion: Destructors, deallocation, and swap must never fail讨论:析构,释放和交换操作必须永不失败Never allow an error to be reported from a destructor, a resource deallocation function (e.g.,operator delete), or aswapfunction usingthrow. It is nearly impossibl...翻译 2021-04-13 19:41:27 · 572 阅读 · 0 评论 -
C++核心准则讨论:将基类的析构函数设为公共和虚拟的,或受保护的和非虚拟的
Discussion: Make base class destructors public and virtual, or protected and non-virtual讨论:将基类的析构函数设为公共和虚拟的,或受保护的和非虚拟的Should destruction behave virtually? That is, should destruction through a pointer to abaseclass be allowed? If yes, thenbase's ...翻译 2021-04-12 21:02:54 · 663 阅读 · 0 评论 -
C++核心准则讨论:如果在初始化期间需要“虚行为”,请使用工厂函数
Discussion: Use a factory function if you need "virtual behavior" during initialization讨论:如果在初始化期间需要“虚行为”,请使用工厂函数If your design wants virtual dispatch into a derived class from a base class constructor or destructor for functions likefandg, you n...翻译 2021-04-10 19:50:18 · 417 阅读 · 0 评论 -
C++核心准则讨论:按照成员声明的顺序定义和初始化成员变量
Discussion: Define and initialize member variables in the order of member declaration讨论:按照成员声明的顺序定义和初始化成员变量Member variables are always initialized in the order they are declared in the class definition, so write them in that order in the constructor.翻译 2021-04-08 19:51:42 · 693 阅读 · 0 评论 -
C++核心准则附录B:代码现代化
Appendix B: Modernizing code附录B:代码现代化Ideally, we follow all rules in all code. Realistically, we have to deal with a lot of old code:理想情况下,我们遵循所有代码中的所有规则。实际上,我们必须处理许多旧代码: application code written before the guidelines were formulated or known.翻译 2021-04-06 20:26:48 · 455 阅读 · 0 评论 -
C++核心准则NL.26:使用传统的常量记法
NL.26: Use conventionalconstnotationNL.26:使用传统的常量记法ReasonConventional notation is more familiar to more programmers. Consistency in large code bases.更多的程序员熟悉常规记法。大型代码库的一致性。Example(示例)const int x = 7; // OKint const y = 9; // bad...翻译 2021-04-05 19:03:59 · 417 阅读 · 0 评论 -
C++核心准则NL.25:不要将void用作参数类型
NL.25: Don't usevoidas an argument typeNL.25:不要将void用作参数类型ReasonIt's verbose and only needed where C compatibility matters.它很冗长,只有在C兼容性很重要的情况下才需要这么做。Example(示例)void f(void); // badvoid g(); // betterNote(注意)Even Dennis R...翻译 2021-03-31 19:09:09 · 500 阅读 · 0 评论 -
C++核心准则NL.21:每个声明(仅)声明一个名称
NL.21: Declare one name (only) per declarationNL.21:每个声明(仅)声明一个名称Reason(原因)Readability. Minimizing confusion with the declarator syntax.可读性。尽量减少与声明符语法的混淆。Note(注意)For details, seeES.10.有关详细信息,请参见ES.10。原文链接hhttps://github.com/isoc...翻译 2021-03-30 18:30:05 · 365 阅读 · 0 评论 -
C++核心准则NL.20:不要在同一行上放置两个语句
NL.20: Don't place two statements on the same lineNL.20:不要在同一行上放置两个语句Reason(原因)Readability. It is really easy to overlook a statement when there is more on a line.可读性。当一行上有更多语句时,很容易忽略某条语句。Example(示例)int x = 7; char* p = 29; // don'ti..翻译 2021-03-28 21:29:57 · 541 阅读 · 1 评论 -
C++核心准则NL.19:避免容易被误读的名称
NL.19: Avoid names that are easily misreadNL.19:避免容易被误读的名称Reason(原因)Readability. Not everyone has screens and printers that make it easy to distinguish all characters. We easily confuse similarly spelled and slightly misspelled words.可读性。并非每个人都在.翻译 2021-03-27 17:48:27 · 437 阅读 · 0 评论 -
C++核心准则NL.18:使用C ++风格的声明符布局
NL.18: Use C++-style declarator layoutNL.18:使用C ++风格的声明符布局Reason(原因)The C-style layout emphasizes use in expressions and grammar, whereas the C++-style emphasizes types. The use in expressions argument doesn't hold for references.C风格布局强调变量在表达式中使.翻译 2021-03-26 19:10:13 · 440 阅读 · 1 评论 -
C++核心准则NL.15:谨慎使用空格
NL.15: Use spaces sparinglyNL.15:谨慎使用空格Reason(原因)Too much space makes the text larger and distracts.太多的空格会使文本变长并分散注意力。Example, bad(反面示例)#include < map >int main(int argc, char * argv [ ]){ // ...}Example(示例)#include &..翻译 2021-03-22 19:23:32 · 440 阅读 · 0 评论 -
C++核心准则NL.16:使用常规的类成员声明顺序
NL.16: Use a conventional class member declaration orderNL.16:使用常规的类成员声明顺序Reason(原因)A conventional order of members improves readability.常规的成员顺序可以提高可读性。When declaring a class use the following order当声明一个类时,使用以下顺序 types: classes, enums, an.翻译 2021-03-20 16:35:31 · 589 阅读 · 0 评论 -
C++核心准则NL.10:首选下划线风格名称
NL.10: Preferunderscore_stylenamesNL.10:首选下划线风格名称Reason(原因)The use of underscores to separate parts of a name is the original C and C++ style and used in the C++ Standard Library.下划线用于分隔名称的各个部分,是C和C ++的原始样式,并在C ++标准库中使用。Note(注意)This rul...翻译 2021-03-17 20:11:55 · 607 阅读 · 1 评论 -
C++核心准则NL.9:全字母大写仅用于宏名称
NL.9: UseALL_CAPSfor macro names onlyNL.9:全字母大写仅用于宏名称Reason(原因)To avoid confusing macros with names that obey scope and type rules.为了避免将宏与遵循范围和类型规则的名称混淆。Example(示例)void f(){ const int SIZE{1000}; // Bad, use 'size' instead in...翻译 2021-03-16 19:08:47 · 581 阅读 · 1 评论 -
C++核心准则NL.8:使用一致的命名方式
NL.8: Use a consistent naming styleNL.8:使用一致的命名方式Rationale: Consistence in naming and naming style increases readability.基本原理:命名和命名样式的一致性可以提高可读性。Note(注意)There are many styles and when you use multiple libraries, you can't follow all thei...翻译 2021-03-15 20:17:05 · 441 阅读 · 0 评论 -
C++核心准则NL.7:使名称的长度与作用域的大小大致成比例
NL.7: Make the length of a name roughly proportional to the length of its scopeNL.7:使名称的长度与作用域的大小大致成比例Rationale: The larger the scope the greater the chance of confusion and of an unintended name clash.原理:范围越大,混淆和意外的名称冲突的机会就越大。Example(示例)d..翻译 2021-03-10 18:33:59 · 447 阅读 · 0 评论 -
C++核心准则NL.5:避免在名称中包含类型信息
NL.5: Avoid encoding type information in namesNL.5:避免在名称中包含类型信息Rationale(基本原理)If names reflect types rather than functionality, it becomes hard to change the types used to provide that functionality. Also, if the type of a variable is changed, cod.翻译 2021-03-09 18:39:47 · 465 阅读 · 0 评论 -
C++核心准则NL.4:保持一致的缩进样式
NL.4: Maintain a consistent indentation styleNL.4:保持一致的缩进样式Reason(原因)Readability. Avoidance of "silly mistakes."可读性。避免“愚蠢的错误”。Example, bad(反面示例)int i;for (i = 0; i < max; ++i); // bug waiting to happenif (i == j) return i;Note..翻译 2021-03-08 18:43:37 · 453 阅读 · 0 评论 -
C++核心准则:注释风格
NL.1: Don't say in comments what can be clearly stated in codeNL.1:请不要在注释中说明代码可以清楚表达的内容Reason(原因)Compilers do not read comments. Comments are less precise than code. Comments are not updated as consistently as code.编译器不阅读注释。注释不如代码精确。注释不会一直随代码一起更.翻译 2021-03-06 19:26:09 · 631 阅读 · 0 评论 -
C++核心准则NL:命名和布局规则
NL: Naming and layout rulesNL:命名和布局规则Consistent naming and layout are helpful. If for no other reason because it minimizes "my style is better than your style" arguments. However, there are many, many, different styles around and people are passi..翻译 2021-03-05 18:58:23 · 649 阅读 · 0 评论 -
C++核心准则GSL.ptr:智能指针相关概念
GSL.ptr: Smart pointer conceptsGSL.ptr:智能指针相关概念 Pointer// A type with*,->,==, and default construction (default construction is assumed to set the singular "null" value) 指针//具有*,->,==和默认构造的类型(假定使用默认构造来设置单数“ null”值) Unique_pointer// A...翻译 2021-03-03 18:37:03 · 469 阅读 · 1 评论 -
C++核心准则GSL.concept:概念
GSL.concept: ConceptsGSL.concept:概念These concepts (type predicates) are borrowed from Andrew Sutton's Origin library, the Range proposal, and the ISO WG21 Palo Alto TR. They are likely to be very similar to what will become part of the ISO C++ standard翻译 2021-03-02 18:33:49 · 437 阅读 · 0 评论 -
C++核心准则GSL.util:实用程序
GSL.util: UtilitiesGSL.util:实用程序 finally//finally(f)makes afinal_action{f}with a destructor that invokesf finally//finally(f)使用调用f的析构函数生成一个final_action {f} narrow_cast//narrow_cast<T>(x)isstatic_cast<T>(x) narrow_cast//n...翻译 2021-03-01 18:44:01 · 585 阅读 · 0 评论 -
C++核心准则GSL.assert:断言
GSL.assert: AssertionsGSL.assert:断言 Expects// precondition assertion. Currently placed in function bodies. Later, should be moved to declarations. //Expects(p)terminates the program unlessp == true//Expectin under control of some options (en...翻译 2021-02-28 18:00:20 · 567 阅读 · 0 评论 -
C++核心准则GSL.owner:所有权指针
GSL.owner: Ownership pointersGSL.owner:所有权指针 unique_ptr<T>// unique ownership:std::unique_ptr<T> unique_ptr <T> //唯一所有权:std :: unique_ptr <T> shared_ptr<T>// shared ownership:std::shared_ptr<T>(a coun...翻译 2021-02-27 20:41:43 · 1321 阅读 · 0 评论 -
C++核心准则GSL.view:视图
GSL.view: ViewsGSL.view:视图These types allow the user to distinguish between owning and non-owning pointers and between pointers to a single object and pointers to the first element of a sequence.这些类型使用户可以区分拥有和不拥有的指针,以及指向单个对象的指针和指向序列的第一个元素的指针。These翻译 2021-02-25 18:40:02 · 585 阅读 · 0 评论 -
C++核心准则GSL:指南支持库
GSL: Guidelines support libraryGSL:指南支持库The GSL is a small library of facilities designed to support this set of guidelines. Without these facilities, the guidelines would have to be far more restrictive on language details.GSL是旨在支持这套准则的小型功能库。如果没有这些功翻译 2021-02-24 18:26:53 · 1507 阅读 · 0 评论