C++
文章平均质量分 77
tsbirdman163
天空没有翅膀的痕迹而我已飞过
展开
-
C++隐式类类型转换
转载于作者 “九天雁翎”老规矩,看个例子,知道我要说的是什么。例4.0:#include #include using namespace std;class Fruit //定义一个类,名字叫Fruit{ string name; //定义一个name成员 string colour;转载 2014-10-24 17:46:36 · 447 阅读 · 0 评论 -
LeetCode--Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to原创 2015-03-07 11:50:49 · 397 阅读 · 0 评论 -
LeetCode--Reverse Nodes in k-Group
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *re原创 2015-03-06 19:48:15 · 401 阅读 · 0 评论 -
LeetCode--Add Two Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *ad原创 2015-03-05 11:47:42 · 470 阅读 · 0 评论 -
LeetCode--Next Permutation
#include #include #include using namespace std;class Solution {public: void NextPermutation(int num[], int n);};void Solution::NextPermutation(int num[], int n){ int change = -1; int原创 2015-03-02 20:01:48 · 410 阅读 · 0 评论 -
LeetCode--找到两个排序数组中第k大的元素
原题目为Median of Two Sorted Arrays,这里被我改成了更为通用的名字和函数。 这是一道非常经典的题。这题更通用的形式是,给定两个已经排序好的数组,找到两者所有元素中第 k 大的元素。O(m + n) 的解法比较直观,直接 merge 两个数组,然后求第 k 大的元素。不过我们仅仅需要第 k 大的元素,是不需要“排序”这么复杂的操作的。可以用一个计数器,原创 2015-02-04 22:29:12 · 8815 阅读 · 1 评论 -
LeetCode--3Sum Closest
3Sum Closest描述Given an array S of n integers, find three integers in S such that the sum is closest to a given number,target. Return the sum of the three integers. You may assume that each input原创 2015-02-04 22:23:00 · 502 阅读 · 0 评论 -
linux 下c++线程池的简单实现(在老外代码上添加注释)
作为一个c++菜鸟,研究半天这个代码的实现原理,发现好多语法不太熟悉,因此加了一大堆注释,仅供参考。该段代码主要通过继承workthread类来实现自己的线程代码,通过thread_pool类来管理线程池,线程池不能够实现动态改变线程数目,存在一定局限性。目前可能还有缺陷,毕竟c++来封装这个东西,资源释放什么的必须想清楚,比如vector存储了基类指针实现多态,那么如何释放对象仍需要考虑,后续我原创 2014-12-11 11:55:43 · 1325 阅读 · 0 评论 -
linux c++线程池的实现(多线程服务器)
本 文给出了一个通用的线程池框架,该框架将与线程执行相关的任务进行了高层次的抽象,使之与具体的执行任务无关。另外该线程池具有动态伸缩性,它能根据执行 任务的轻重自动调整线程池中线程的数量。文章的最后,我们给出一个简单示例程序,通过该示例程序,我们会发现,通过该线程池框架执行多线程任务是多么的简 单。为什么需要线程池目前的大多数网络服务器,包括Web服务器、Email服务器以及数据库转载 2014-12-09 17:18:56 · 1916 阅读 · 0 评论 -
C++ 泛型句柄类
#include #include using namespace std;template class handle{public: handle(T* p):use(new int(1)),ptr(p){} handle(handle& h) //复制构造函数不需要考虑指针的移除问题,因为对象刚被新建,指针没有旧的指向,因此不需要释放之前的空间。 { ptr = h.原创 2014-12-03 23:00:09 · 621 阅读 · 0 评论 -
C++ 实现自己queue模板类
按照书上写了差不多的例子,程序一写发现好多语法问题,需要继续学习。#include using namespace std;template class queue;template class item{ friend class queue;private: item(const T& val):value(val),next(0){} //带有一个参数的构造函数原创 2014-12-03 12:39:10 · 1480 阅读 · 0 评论 -
C++关联容器的使用实例
#include #include #include #include #include #include #include using namespace std;class TextQuery{public: typedef vector::size_type line_no; TextQuery(){}; void read_file(ifstream &is)原创 2014-11-05 16:43:15 · 751 阅读 · 0 评论 -
vector的reserve和resize
vector 的reserve增加了vector的capacity,但是它的size没有改变!而resize改变了vector的capacity同时也增加了它的size!原因如下: reserve是容器预留空间,但在空间内不真正创建元素对象,所以在没有添加新的对象之前,不能引用容器内的元素。加入新的元素时,要调用push_back()/insert()函数。 r转载 2014-11-05 18:51:17 · 426 阅读 · 0 评论 -
c++ 转换与类类型简单实例
在需要做数据类型转换时,一般显式的写法是:[cpp] view plaincopy"font-size:18px;">type1 i; type2 d; i = (type1)d; //显式的写类型转,把d从type2类型转为type1类型 这种写法不能做到无缝转换,也就是直接写 i = d,而不需要显式的写(typ转载 2014-11-01 10:42:06 · 581 阅读 · 0 评论 -
Boost智能指针——weak_ptr
Boost智能指针——weak_ptr循环引用:引用计数是一种便利的内存管理机制,但它有一个很大的缺点,那就是不能管理循环引用的对象。一个简单的例子如下:#include #include #include #include class parent;class children;typedef boost::shared_ptr parent_转载 2014-11-16 21:40:25 · 438 阅读 · 0 评论 -
指针指针--shared_ptr简单实用方法
今天晚上去旁听了C++高级编程的课,其中提到智能指针。第一反映还以为是auto_ptr呢,一听才知道是share_ptr这个。哦,原来是C++11特性。大致的原因是auto_ptr有一点缺陷,而share_ptr比较安全。好吧,那就这个了。 先写一个简单的代码感受一下 复制代码 1 #include 2 #include 3 4 us转载 2014-11-16 21:16:35 · 921 阅读 · 0 评论 -
C++: 拷贝构造函数
一. 什么是拷贝构造函数首先对于普通类型的对象来说,它们之间的复制是很简单的,例如:[c-sharp] view plaincopyint a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量。下面看一个类对象拷贝的简单例子。转载 2014-10-28 10:07:43 · 495 阅读 · 0 评论 -
C++实现一个自己的string类
class String{public: String(const char *str = NULL);// 普通构造函数 String(const String &other); // 拷贝构造函数 ~ String(void); // 析构函数 String & operator =(const String &other);// 赋值函数private: ch原创 2015-03-30 21:26:19 · 3127 阅读 · 0 评论