自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(38)
  • 收藏
  • 关注

原创 modern effective C++ 18

1.概述 该条款是关于智能指针的,unique_ptr主要用来管理互斥的资源,经常用来对C++中的强聚合关系建模,一个类可以把另外一个类当做成员,这样两个类的生命周期完全相同,大类完全掌控了小类的构造和析构,但是该类的内存需求相对较大,可以采用指针来管理类的成员函数,使用RAII在构造中new 在析构时delete,同样可以使用unique_ptr在构造时分配,当类生命周期结束时,会自动调用析构函

2017-12-26 19:10:57 250

原创 C++ bind

1.概述 function是C++11中对于函数,函数指针,函数对象,lambda表达式等可调用函数的汇总,bind函数将一个普通函数或者成员函数转化为可给function对象赋值的对象,使用这两个函数可以实现C++通用意义上的回调和委托机制.2.bind 函数返回的函数对象赋值给function对象时的要求是,返回值一定要相同,参数类型需要相同,参数个数需要小于等于function函数对象的参数

2017-11-21 20:26:14 217

原创 connect系统调用

1.概述 linux网络编程中,一般是客户端调用connect函数,该函数将引起操作系统的tcp协议向远端的服务器执行三次握手的过程,分为两种情况进行说明,一种是connect的第一个参数文件描述符是非阻塞的.一种是该文件描述符是阻塞的. 2.阻塞情况 当该文件描述符是阻塞的时候,那么connect调用也必然是阻塞的,只有当连接上或者连接失败时返回,意味着connect返回时仅有两种情况,已经

2017-11-16 21:12:43 841

原创 Effective 阅读笔记---条款29"为异常安全而努力是值得的"

1.概述 异常安全的函数有两个必要条件; (1)不泄漏任何资源:当异常被抛出时,必须保证资源得到泄漏,解决办法是使用RAII 手法.书中的例子是互斥锁的例子,具体做法是将加锁的操作封装到一个类的构造函数中,而把解锁的操作封装到析构函数中,当发生异常时一定会调用析构函数执行解锁. 再一个是使用智能指针管理堆上的内存,也是RAII手法的使用. (2)不允许数据败坏. 2. 异常安全函数提供以

2017-11-16 20:44:15 201

原创 Effecitve 阅读笔记--private继承与组合

Effective 条款38和39都是关于has a关系的讨论. 1.private继承则是其中的一种has a 关系. .private继承的语义是implements-in-terms-of. 如果你让一个类private继承另外一个类,你的用意是为了采用父类一些已有的实现,不是因为两个类在任何观念上的关系.private继承纯粹只是一种实现技术,(这就是为什么继承自一个private bas

2017-11-05 14:04:10 275

原创 Effective C++读书笔记一 条款36 绝对不要重新定义public继承来的non-virtual函数

Effective C++36 never refine inherited non-virtual function1.实验如果违反该条规则会如何#include <iostream>using namespace std;class B{ public: void mf(){ cout<<"base"<<endl; }};cl

2017-11-05 11:42:02 213

原创 C++细节点记录

1.概述 看effective C++ 中有一条款倡导const 引用来代替按值来向函数传递参数,总结起来的好处主要有: 避免了对象的拷贝, 引用能够实现多态,能保证在函数中调用到正确的方法.也即是解决了割裂的问题. 然后又介绍了必须返回对象时,不能用引用来传递,给出了一些例子,包括返回栈对象引用,堆对象引用 和静态对象引用等会带来的问题,指出这种情况下必须要返回对象,让编译器进行一些优化工

2017-11-04 21:10:50 192

原创 Poll与Epoll 区别总结

1.概述 网上收集了一下poll和epoll的实现原理,写下来以备后验. 2. 从api上面看,select和poll每次调用的时候需要用户自己整理该次调用关注的文件描述符和事件,内核是不会帮助用户维护前面已经关注过的记录. 而epoll是增量式的管理,每次需要告诉内核我现在关心的哪个文件描述符,内核查询以往的记录,整合起来. 打个比方就像出去购物,select和poll老板就不怎么好,每次去的

2017-11-04 11:35:26 5850 1

原创 C++语言细节点

1.概述 最近写C++代码的时候发现一些小的细节点,写下来加深理解,以后也好查看. 2.C++编译问题 一般不应该在.h文件中声明全局变量,因为如果两个.cpp文件同时include该头文件后,那么就会报重定义问题.但我们确实需要在其他的cpp文件中使用该变量,解决办法是将这些变量定义在a.cpp文件中,在b.h文件中使用extern, 在c.cpp中可以include b.h ,编译好c后

2017-11-02 22:23:44 266

转载 C++ 使用std::function 和std::bin实现委托

1.概述 C++11提供的function 和bind函数可以为C++的类之间提供委托,function相当于融合了各种函数,函数指针,lamda表达式还有函数对象等可调用,可回调函数. 抽象出它们共有的属性即只要返回值类型和参数类型一样的可调用对象,不管它是函数,函数指针,还是函数对象都可以被function 统一起来.bind函数返回一个匿名的函数对象,对其sizeof运算发现其占用内存.

2017-10-23 22:54:28 1158

原创 linux线程同步-条件变量

多线程环境下,如果多个线程对同一共享资源并发访问,那么由于访问资源的时候,由于访问资源的操作的非原子性,可能在向一个buffer写入数据时,另外一个线程也在写入buffer 这时候可能由于两条线程的交替执行的时机的随机性而使得结果每一次运行都不同,这时候就需要操作系统提供的互斥机制来保证某一时刻只有一个线程访问临界区代码,这就是互斥量的语义. 互斥是同步的特殊情况,相当于一个线程告诉另外一个线程,我

2017-10-22 22:14:49 692

原创 nginx源码阅读笔记.array和list数据结构

1.概述 nginx中,array和list 的实现 和queue的实现不同,queue的实现是不依赖于具体的结构的,可以申明任何结构体只要该结构体中间含有queue的实例就可以将它们组织起来,那么queue的对象的内存分配需要(用户自己申请),在分配的包含queue的对象时已经分配好, 而list和array 的实现则时初始化一个list或者array, 自定义的结构的内存分配任务交给list

2017-10-18 22:13:16 203

原创 nginx数据结构ngx_queue_t

1.概述 nginx为了跨平台底层封装了自己的数据结构,今天学了ngx_queue_t这个数据结构,这个结构实现了双向队列,该双向队列可以应用到任意结构体中把相应的结构体按照队列的方式组织起来.2.代码/* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */#include <ngx_config.h>#include

2017-10-18 19:20:05 313

原创 libevent源码阅读笔记

1.概述 libvent是基于事件驱动的开源框架.记录一下看源码学到的东西. 2. 核心结构体.struct event { TAILQ_ENTRY (event) ev_next; TAILQ_ENTRY (event) ev_active_next; TAILQ_ENTRY (event) ev_signal_next; unsigned int min_he

2017-10-18 00:11:10 451

原创 libevent数据结构尾队列

1.概述 最近找来libevent的源码学习了一下,看到用宏实现的数据结构甚是惊讶,把其中尾队列的代码copy出来test了一下.个人感觉用宏实现该队列的思想是C++的模板的思想.我自己实现一个队列的思路肯定是写一个队列 中节点的结构体然后针对该结构体实现队列的基本操作.这样的队列只能使用于写好的节点….而libevent实现的版本是不依赖于实际的节点的.换句话说,我们有两个类,一个student

2017-10-17 12:38:05 330

原创 linux进程间通信之共享内存

1.概述 共享内存是最快的进程间通信方式,系统在内存中维护一个特殊大小的内存块,进程可以将这快内存使用自己的页表映射到自己的内存空间中,则该内存就可以被两个进程访问实现了通信的功能.而且建立映射关系后传输数据时不用再进入内核,相比于消息队列而言更快. 2.基本数据结构struct shmid_ds {struct ipc_permshm_perm;/* operation perms */

2017-10-16 16:50:11 174

原创 linux进程间通信之信号量

1.概述 以前上课老师说信号量是二十世纪操作系统领域最伟大的发明之一,另外一项技术是虚拟内存.个人感觉linux系统中的锁的机制的实现应该都与信号量相关,先写下来不管对错, 互斥锁感觉就是0 -1 的二元信号量,其实这两者都依赖于底层的原子的操作来帮它们实现p v 操作. 2.linux中信号量有关的数据结构struct sem {short sempid;/* pid of last o

2017-10-16 12:06:04 182

原创 linux进程间通信---消息队列

1.概述 消息队列是SYSTEM V实现的进程间通信方式,linux操作系统支持这种通信方式,内核需要为每一个消息队列维护一系列数据结构. 2.消息队列所设计的数据结构struct msg {struct msg *msg_next; /* next message on queue */long msg_type;char *msg_spot;/* message text addre

2017-10-15 20:53:10 230

原创 linux 进程间通信之管道和有名管道

管道经常用于有亲缘关系间的进程间通信,管道pipe借助于文件系统的接口,但其实管道的实现应该不在文件系统中,只是实现了文件系统系统的接口,利用进程在fork时,子进程会继承父进程的进程描述符,实际在fork时,内核中对应的打开的文件的引用计数会增加1,父子进程都可以访问相同的文件. 管道也是一种单向通信的方式,即半双工,父进程关闭读端,子进程关闭写端,则父进程向管道的一端写,子进程会在管道的另外一

2017-10-15 18:31:42 292

原创 linux 信号笔记

1.linux信号是一种古老的进程间通信的方式,唯一一种异步通信的方式,信号的默认处理方式一般是结束当前进程如SIGINT,SIGQUIT等,应用进程可以自己控制进程处理的方式和流程. 2.信号的注册 使用signal函数或者sigaction函数注册, 3.信号是不可靠的进程间通信方式,在信号的执行例程中不能被同一种信号打断,但是会被进程接收到的其他信号所打断,#include <stdio.

2017-10-15 11:18:41 371

翻译 精确4SAT问题np完全性证明

1.题目描述 在精确的4SAT问题中,输入为一组子句,每个子句都是恰好4个文字的析取,且每个变量最多在每个子句中出现一次.目标是求它的满足赋值—–如果该赋值存在.证明精确的4SAT问题是NO-完全问题. 2证明过程 通过将 3SAT 归约到 EXACT 4SAT 来证 明后者的 NP 完全性。对于任意一个 3SAT 实例,如果其中某个子句中包含了同一个 文字多次,那么可以缩减为一次,如果同

2017-07-02 16:54:51 578

翻译 Ones and Zeroes

1.问题描述 In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.For now, suppose you are a dominator of m 0s and n 1s respectively. On the o

2017-06-28 14:56:38 216

翻译 Target Sum

1.问题描述 You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.Find out h

2017-06-27 22:43:08 167

翻译 Friend Circles

1.问题描述 There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of

2017-06-27 22:38:13 172

翻译 Bulb Switcher

1.问题描述 There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turni

2017-06-11 15:58:53 171

翻译 Count Numbers with Unique Digits

1问题描述 Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10的n次方.Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 10

2017-06-04 20:22:27 188

翻译 Increasing Triplet Subsequence

1问题描述 Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should:Return true if there exists i, j, ksuch that arr[i] < arr[j

2017-05-21 21:10:43 150

翻译 Counting Bits

1 问题描述 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.Example: For num =

2017-04-26 21:52:29 172

翻译 Linked List Random Node

1 问题描述 Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen.Follow up: What if the linked list is extremely large an

2017-04-15 14:14:26 168

翻译 Linked List Cycle II

1问题描述 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 2思路 求解链表中的环,以及环起点位置,使用标准的floyd龟兔赛跑的问题 2.1使用一个慢指针和一个快指针,如果没有环的存在快慢指针将永远都不会再次相等. 2.2计算环的起点位置,假设

2017-04-11 10:08:29 216

翻译 Binary Search Tree Iterator

1.问题描述 Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()

2017-04-10 21:06:13 165

翻译 Binary Tree Right Side View

问题描述 Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example: Given the following binary tree,1

2017-04-10 18:36:35 174

翻译 Unique Binary Search Trees

1.问题描述 Given n, how many structurally unique BST’s (binary search trees) that store values 1…n? For example, Given n = 3, there are a total of 5 unique BST’s.1 3 3 2 1 \

2017-03-31 20:16:00 195

翻译 Longest Substring Without Repeating Characters

1 问题描述 Given a string, find the length of the longest substring without repeating characters.Examples: Given “abcabcbb”, the answer is “abc”, which the length is 3. Given “bbbbb”, the answer is “b”,

2017-03-26 20:41:04 213

翻译 Group Anagrams

1 问题描述 Given an array of strings, group anagrams together.For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Return: [ [“ate”, “eat”,”tea”], [“nat”,”tan”], [“bat”] ] 2解决思路

2017-03-18 20:32:36 250

翻译 Minimum Moves to Equal Array Elements

1问题描述 Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected eleme

2017-03-12 20:24:17 167

翻译 Minimum Moves to Equal Array Elements

1 问题描述 Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected elem

2017-03-12 20:07:12 155

翻译 Predict the winner

1 问题描述 Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time

2017-02-26 16:14:33 241

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除