
C++
文章平均质量分 62
徐九五
学生
展开
-
LeetCode OJ刷题历程——Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2016-03-02 12:00:04 · 397 阅读 · 0 评论 -
《深入探索C++对象模型》第四章:Function语意学
1、Member的各种调用方式在C++中,支持三种类型的member functions:static、nonstatic和virtual,每一种类型被调用的方式都不一样,下面我们分别来探讨一下:(1)NonstaticC++要求非静态成员函数必须要和一般的非成员函数具有相同的效率。采取的措施是通过以下步骤将成员函数实例转换为对等的非成员函数实例:a.改写函数的原型(signatu原创 2017-06-24 15:39:00 · 405 阅读 · 0 评论 -
muduo网络库源码解析 二
这一篇我们来看源码,首先分析EventLoop类,该类继承自UnCopyable:class UnCopyable{public: UnCopyable() = default; UnCopyable(const UnCopyable &obj) = delete; UnCopyable& operator=(const UnCopyable &rhs) = delete;};由于原创 2017-07-04 10:52:47 · 458 阅读 · 0 评论 -
muduo网络库源码解析 三
这一节我们来解析Reactor最核心的事件分发机制——Channel和Poller。每个Channel对象只属于一个EventLoop,因此每个Channel对象都只属于一个IO线程,并且只负责一个文件描述符,但是并不拥有这个fd,也不会在析构的时候关闭这个fd。首先来看一下构造函数原型Channel(EventLoop *loop, int fd);很明显,需要说明所属EventLoop,以及负原创 2017-07-04 11:37:16 · 348 阅读 · 0 评论 -
《剑指Offer》第五章
第一题:数组中出现次数超过一半的数字仔细分析一下,这个数字必定是数组的中位数。可以利用快排的思想int MoreThanHalfNum(vector &nums){ int length = nums.size(); int middle = length >> 1; int start = 0; int end = length - 1; int index = Partit原创 2017-07-10 22:03:38 · 271 阅读 · 0 评论 -
muduo网络库源码解析 四
这一章节我们首先来解析用到的socket的基础函数:1、字节序转换函数封装为HostToNetwork16、HostToNetwork32类似命名,原函数为htons、htonl,封装后方便记忆与书写,头文件2、地址转换函数toHostPort和fromHostPort,用的是中的inet_pton和inet_ntop,p和n表示表达式和数值3、设置文件描述符为non-blocking原创 2017-07-05 11:23:28 · 344 阅读 · 0 评论 -
《Effective C++》第一章:让自己习惯C++
条款一:视C++为一个语言联邦四大联邦:C、Object-Oriented C++、Template C++、STL条款二、三:尽量以const,enum,inline替换#define此条款旨在阐述“尽量以编译器代替预处理器”这个概念。我们首先带分析一下#defined的不好之处:(1)利用#define定义一个常量,比如#define PI 3.14,记号名称PI也许从未被编译原创 2017-06-26 15:57:53 · 306 阅读 · 0 评论 -
《Effective C++》第二章:构造/析构/赋值运算
条款5:了解C++默默编写并调用哪些函数一般情况下,编译器会默默编写一个default 构造函数、default 析构函数、拷贝构造函数、赋值函数,这个问题在《深入探索C++对象模型》的时候也说过,编译器只有当这些函数被编译器需要的时候,才会被创建调用。关于赋值函数,我们需要注意一点,如果你打算在一个“内含reference成员或者const成员”的class内支持赋值操作,你必须自己定义原创 2017-06-26 17:16:51 · 611 阅读 · 0 评论 -
《Effective C++》第四章:设计与声明
条款20:宁以pass-by-reference-to-const替换pass-by-value这个条款其实也可以用pass-by-pointer-to-const来替代。因为如果传值,对于用户自定义类型来说十分费时,而使用const类型的引用或者指针来传递,不仅省时,而且保护了原对象不会被修改。但是对于内置类型、STL的迭代器(不是很懂)来说,传递value往往更合适。条款21:必须返回原创 2017-06-27 15:57:40 · 369 阅读 · 0 评论 -
《Effective C++》第六章:继承与面向对象设计
条款32:确定你的public继承塑模出is-a关系is-a的关系就是说派生类一定是一个基类,额,通俗一点说就是派生类可以当做基类一样来使用。即本章阐述的观点就是,public继承就意味着is-a。适用于base classes身上的每一件事一定也适用于derived classes身上,确保base能做的事,derived也一定可以做条款33:避免遮掩继承而来的名称考虑下面这个例子:原创 2017-06-27 17:56:49 · 356 阅读 · 0 评论 -
《深入探索C++对象模型》第三章:Data语意学
我们先来看一个问题,关于菱形继承:class top{};class left:public virtual top{};class right:public virtual top{};class bottom:public left,public right{};那么,我想问的是 这四个class的大小分别是多少?我在g++ 4.8上做了验证(64位系统):这个结原创 2017-06-23 17:28:29 · 263 阅读 · 0 评论 -
《Effective C++》第三章:资源管理
条款13:以对象管理资源主要的思想是C++的“析构函数自动调用机制”,即栈上的变量会在离开作用域后自动调用析构函数。但是对于heap上的变量,编译器不会自动调用析构函数,通常需要手动调用delete。标准款利用这个思想提供了智能指针,把资源放入对象内,对象的析构函数调用delete。(1)获得资源后立即放入对象进行管理。即所谓的资源获取即初始化(2)运行析构函数确保资源被释放下面我原创 2017-06-27 14:14:04 · 318 阅读 · 0 评论 -
muduo网络库源码解析 五
TcpConnection用来管理连接,包括连接的状态、消息的处理(发、收),该类是muduo中唯一一个实用shared_ptr来进行管理的类,也是唯一继承enable_shared_from_this的类,这源于TcpConnection类的模糊的生命期。在本章的结尾我们会进行分析。我们首先来分析一下私有成员:enum StateE { kConnecting, kConnected,原创 2017-07-06 15:04:57 · 416 阅读 · 0 评论 -
《剑指Offer》第三章
第一题:数值的整数次方需要注意的地方:指数为负数,指数为负数的时候,数值为0,首先给出一般性的解法://注意,判断浮点数是否相等,不能用==,而是差值在某个范围内bool equal(double num1, double num2){ if (num1 - num2 > -0.0000001 && num1 - num2 < 0.0000001) return true; e原创 2017-07-06 17:21:56 · 255 阅读 · 0 评论 -
《Effective C++》第七章:模板与泛型编程
条款41:了解隐式接口和编译期多态这个隐式接口....我也没搞懂。编译期多态到是容易理解,函数重载和template的具现化条款42:了解typename的双重意义这个在STL中用的比较多,特别是traits技术中。嵌套一个类型,使用的时候必须用typename来进行声明,这是一个类型:templatestruct myTraits{ typedef T value_t原创 2017-06-28 14:21:03 · 289 阅读 · 0 评论 -
《深入探索C++对象模型》第二章:构造函数语意学(下)
1、程序转化语意学(1)参数的初始化把一个class object当做参数传给一个函数,相当于以下形式的调用:原创 2017-06-22 18:04:19 · 314 阅读 · 0 评论 -
《深入探索C++对象模型》第二章:构造函数语意学(上)
1、Default Constructor的构造操作default constructor什么时候会被合成?——在被需要的时候......那么是谁需要呢?需要做什么呢?书中说不是程序需要,而是编译器需要,程序如果有需要,那是程序员的责任。总的来说,对于class X,如果没有任何user-declared constructor,那么会有一个default constructor被隐式的声原创 2017-06-22 17:02:11 · 298 阅读 · 0 评论 -
《深入探索C++对象模型》第一章:关于对象
1、封装后的成本会增加吗?对于一个普通的class,即不含虚函数,没有继承virtual base class(虽然通常不含有虚函数说明不存在继承体系),封装后,并没有增加成本,data members直接放在object内,就像在struct里的表现一样,而member functions虽然在class内声明,却不出现在object里(这个后面会说到,其实函数会被签名化,转换为nomemb原创 2017-06-22 11:47:40 · 315 阅读 · 0 评论 -
LeetCode OJ刷题历程——Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321以上是题目要求。本题较为Easy,但是值得注意的细节不少。下面贴上代码:class Solution {public: int reverse(int x) { long原创 2016-03-06 13:43:04 · 361 阅读 · 0 评论 -
LeetCode OJ刷题历程——Sting to Integer(atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.原创 2016-03-06 13:59:42 · 333 阅读 · 0 评论 -
LeetCode OJ刷题历程——Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.以上为题目要求。首先说说这个题目我的解题思路,首先找出string数组中长度最短的任意一个string,以此作为基准来寻找最长的相同前缀。下面贴出代码:class Solution {public: s原创 2016-03-06 14:07:13 · 270 阅读 · 0 评论 -
LeetCode OJ刷题历程——Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked list becomes 1原创 2016-03-06 14:12:44 · 277 阅读 · 0 评论 -
LeetCode OJ刷题历程——Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2016-04-03 20:22:12 · 290 阅读 · 0 评论 -
LeetCode OJ刷题历程——Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one dig原创 2016-04-03 20:28:45 · 454 阅读 · 0 评论 -
LeetCode OJ刷题历程——Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Subscribe to see which companies asked原创 2016-04-03 20:31:44 · 399 阅读 · 0 评论 -
LeetCode OJ刷题历程——Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your f原创 2016-04-03 20:37:41 · 406 阅读 · 0 评论 -
LeetCode OJ刷题历程——Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is原创 2016-04-03 21:49:59 · 340 阅读 · 0 评论 -
LeetCode OJ刷题历程——Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element alwa原创 2016-04-03 22:04:40 · 350 阅读 · 0 评论 -
LeetCode OJ刷题历程——Two Sum
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].You may assume that each input would have exactly one solution.以上是题目要求。这是LeetCode的第一题,没什么难度,我选择原创 2016-03-01 10:09:33 · 366 阅读 · 0 评论 -
《STL源码剖析》之Traits
不论是泛型思维或STL的实际运用,迭代器都扮演着重要的角色。STL的中心思想在于:将数据容器和算法分开,彼此独立设计,最后再以一帖强力胶将他们粘在一起。容器和算法的泛型化,从技术角度来看,并不困难,C++的 class templates和function templates可分别达成目标。而迭代器正是这一帖强力胶。但是,在实现泛型算法的时候,虽然有template,但是对于同一功能,不同迭代原创 2017-03-28 16:23:28 · 243 阅读 · 0 评论 -
《STL 源码剖析》之空间配置器
首先,这个为什么叫“空间配置器”而不叫“内存配置器”呢?因为空间不一定是内存,也可以是磁盘或者其它存储介质。下面我们来看一段代码: class Foo{ ... }; Foo *p = new Foo; delete p;这在C++中是很普遍的内存申请与释放操作。其中new算式内含两个阶段的操作:(1)调用::operator new配置内存,即申请内存(2原创 2017-03-27 15:00:03 · 276 阅读 · 0 评论 -
muduo网络库源码解析 七
这一章节,我们来分析多线程TcpServer,首先分析muduo one loop per thread的基石——EventLoopThread class,EventLoopThread会启动自己的线程,并在其中运行EventLoop::loop(),关键的startLoop()函数定义如下,此函数会返回新线程中的EventLoop对象的地址:EventLoop* EventLoopThre原创 2017-07-11 10:55:18 · 667 阅读 · 0 评论 -
muduo网络库源码解析 六
本章节我们来解析Buffer类(应用层缓冲区)的设计以及TcpConnection接收和发送数据。我们首先来回顾一下muduo的IO模型:one loop per thread + IO multiplexingevent loop是non-blocking网络编程的核心,而non-blocking几乎总是和IO multiplexing一起使用:(1)没有人真的会使用轮询来检查某个n原创 2017-07-10 16:39:28 · 604 阅读 · 0 评论