C++
文章平均质量分 62
xanarry
这个作者很懒,什么都没留下…
展开
-
c++中友元类的使用
#include using namespace std;class Date;//因为在Time中要用Date类,但是还没有定义,所以先声明class Time{private: int hour; int min; int sec;public: Time(int h, int m, int s):hour(h), min(m), sec(s){} void disp原创 2014-04-20 14:31:52 · 1114 阅读 · 0 评论 -
高精度大整数类
寒假写的高精度大整数类,在此分享给大家,方法就是将数字转为字符串,然后加减乘过程都是模拟列竖式运算,除法方法神奇一点,粗看代码可能看不懂除法的运算,准确的说是取整,后面我将专门写一篇文章介绍大整数的除法即取整运算的文章#include <iostream>#include <cstring>#include <string>using nam...原创 2015-03-20 23:23:51 · 741 阅读 · 2 评论 -
使用c++构建一个二叉树
How to create a binary tree by c++ The binary tree is a fundamental data structure used in computer science. The binary tree is a useful data structure for rapidly storing sorted data and rapidly翻译 2014-11-09 13:18:46 · 4712 阅读 · 0 评论 -
计算fibonacci数列logn的算法
计算fibonacci数列通常有两种方法,非常符合fibonacci数列公式表达的方法就是使用递归,当x = 1或x = 2时,fab(x) = 1; x > 2时,fac(x) = fac(x - 1) + fac(x - 2);int fibonacci(int n){ if (n <= 2) return 1; else return f原创 2015-04-10 10:57:18 · 1338 阅读 · 0 评论 -
c++使用顺序结构实现栈
关于栈的介绍在此不用多说了,想必在原创 2014-06-05 22:35:24 · 726 阅读 · 0 评论 -
队列-链式结构-C++实现
教科书告诉我们所先进先出的结构叫做队列,没错,就是队列,正如排队一样了,出队永远在队头,入队永远在队尾,我们要频繁对这两个点进行操作,那么应该选择什么样的结构来实现呢?数组,特点:实现简单,容量有限,删除一次要进行一定数量元素的移动,所有效率有待提高。当然可以循环使用数组空间来避免移动元素带来的开销,但是呢,还是不如链式结构来得直观。 本文使用一个双向链表来实现队列,用两个固定的原创 2015-04-16 22:08:42 · 2421 阅读 · 0 评论 -
创建一个有序的链表
创建有序链表的基本思路就是在插入一个新节点之前,先按有序查找到新节点应该插入的位置,然后再插入!//代码实现环境:windows 7 32bit + MinGW + Sublime Text 3#include <iostream>using namespace std;struct Node{ int elem; Node *next;...原创 2014-06-05 22:50:04 · 6869 阅读 · 4 评论 -
c++实现链式结构的栈
链式结构的栈的主题是一个单向链表,因为只有一个可访问的头并且可以轻松在头部进行插入和删除,因为栈的操作也都是在顶部,所以可以根据这一特性用链表的头部表示栈的顶部实现一个链式结构的栈,并且不存在大小的限制。更多栈的特性不做过多描述。 我们使用带头结点的链表,头结点的数据区就储存栈的元素个数,然后实现链表的头部插入函数模拟栈的push,实现链表的删除头部元素模拟栈的pop。原创 2015-04-16 21:38:01 · 1077 阅读 · 0 评论 -
c/c++获取系统时间
使用以下代码即可获取系统时间。#include #include #include int main(){ time_t t = time(0); char tmp[200]; strftime(tmp, sizeof(tmp), "%c",localtime(&t));//其中%c是时间格式化形式的参数,其可选参数非常多,具体参考cplusplus.com原创 2015-04-17 19:45:34 · 740 阅读 · 0 评论 -
使用栈判断括号匹配
判读括号是否匹配的方法比较简单,假如给定一系列的括号,判断是否全部都匹配我们肯定是将能匹配的括号全部删除,如果最后没有剩下任何括号那么肯定就是匹配了;如有剩下的左括号或者右括号说明不匹配。 详见代码描述:#include #include using namespace std;int main(int argc, char const *argv[]){ s原创 2015-04-17 20:42:57 · 3119 阅读 · 0 评论 -
拓扑排序
关于拓扑排序我有个印象就是最开始是个(同专业,不同大学)高中同学问我能不能帮她写个拓扑排序。那时我还是不会的,当然,知道放假后假期我才会了。首先介绍一下什么是拓扑排序吧,通常我们描述的排序是按数据的大小排序,并且各种算法,各种奇巧淫技。拓扑排序最大的区别就是它并不是按照数据的大小排序的,我把理解为是对一种先后关系之间的排序,举个例子。 假如你对你的女神表白了,很不幸,女神是学原创 2015-04-17 11:12:08 · 656 阅读 · 0 评论 -
C++ STL 使用总结
相对于c,c++的标准模板库也就是STL通过了很多丰富的数据结构和算法,熟练运用能给编程带来极大的方便,下面介绍STL中常用数据结构的简单用法原创 2015-03-18 23:40:12 · 476 阅读 · 0 评论 -
c/c++获取系统时间
/方案— 优点:仅使用C标准库;缺点:只能精确到秒级#include #include int main( void ) { time_t t = time(0); char tmp[64]; strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );转载 2014-09-23 19:50:43 · 707 阅读 · 0 评论 -
C++拷贝构造函数详解
一. 什么是拷贝构造函数首先对于普通类型的对象来说,它们之间的复制是很简单的,例如:[c-sharp] view plaincopyint a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量。下面看一个类对象拷贝的简单例子。[c-sharp] view pla转载 2014-06-19 09:14:22 · 436 阅读 · 0 评论 -
c++类模板的简单使用
#include #define elmtpe char//在此修改要处理的数据类型using namespace std;/*///////////引用的函数原型//////////////class Compare{private: int x, y;public: Compare(int a = 0, int b = 0):x(a), y(b){} int Max()原创 2014-04-20 16:30:56 · 1742 阅读 · 0 评论 -
c++将整个数组中的元素转移到vector模板类对象中
#include #include using namespace std;using std::vector;int main(int argc, char const *argv[]){ int a[20];//define a integer array has 20 elements; cout << "The elements of a[20] is as follow原创 2014-05-07 22:37:41 · 2836 阅读 · 0 评论 -
C++中指定小数位数输出
C++中指定小数位数输出按有效位输出是 setprecision,按小数位数输出也是setprecision,但到底是谁取决于fixed。测试代码:#include #include using namespace std;int main( void ){ const double value = 12.3456789; cout << value转载 2014-04-24 22:33:55 · 3681 阅读 · 0 评论 -
猴子选大王(约瑟夫环问题)
#include using namespace std;struct Node//链表节点结构体{ int n;//节点数据 Node *next;//一个结构体指针};Node * Create(const int);//声明创建链表的函数void Conduct(Node *, const int ,const int);//声明筛选结果的函数void free(Nod原创 2014-05-11 22:02:37 · 902 阅读 · 0 评论 -
一个c++多重继承的例子
/*一个多重继承的例子,以下的所有函数均直接写在类中*/#include #include using namespace std;/*定义Hdd类表示硬盘*/class Hdd{private: int rpm;//转速 int size;//容量 int cache;//缓存public: Hdd(int r, int s, int c) { rpm = r;原创 2014-04-27 16:31:36 · 3779 阅读 · 0 评论 -
创建一个有序链表
#include using namespace std;struct Node//节点结构体{ int n; Node *next;};void Insert(Node *H, int n);//有序插入节点函数void Show(Node *H);//输出链表int main(int argc, char const *argv[]){ int n; Node *原创 2014-05-19 17:12:21 · 3158 阅读 · 0 评论 -
for循环中++i与i++的执行效率与区别
for(int i=0;i 我们经常会使用这样的代码来做循环,一般在for循环中习惯于使用i++,却很少使用++i。虽然从代码执行的效果来看,它们并无任何区别,但是它们所生成的代码是有有所不同的。 i++ :先引用后增加++i :先增加后引用 意思就是说就是 i++ :先在i所在的表达式中使用i的当前值,后让i加1转载 2014-05-19 20:56:01 · 1181 阅读 · 0 评论 -
char *p与char p[N]的一点思考
今天用c++写了一个非常简单的程序 结果 很失败 因为自己编程比较少 所以有时候还会 c与c++混用 今天写了这个#include using namespace std;int main(){char *a;cin>>a;cout<<a;}在c语言里 char *p 再scanf()这个很正常 可是 在c++里 编译通过了 但是运行出错转载 2014-05-19 21:38:42 · 1344 阅读 · 0 评论 -
使用栈实现判断输入的符号是否匹配
/* date: 2014/6/5 time: 20:07 运行环境:windows 7 + MinGW + Sublime_text_3 注意:此代码不能判断符号相互交叉的匹配,比如({)},只能测试{([])({})}类型的*/#include #include #include using namespace std;struct Stack{ int size;原创 2014-06-05 22:17:04 · 2252 阅读 · 0 评论 -
队列的简单介绍及代码实现
队列的结构就是先进先出,经相当于原创 2014-06-06 14:57:55 · 823 阅读 · 0 评论 -
AVL树的c++实现
//avl树中的元素一定是唯一的#include <iostream>using namespace std;struct Node{ int data;//数据 Node *left;//左指针 Node *right;//右指针 int height;//当前点的树高};int height(Node *root){ if (!root)//nu原创 2016-02-27 21:11:09 · 418 阅读 · 0 评论