C++
好小葱1
周末不卡带,人生不卡带。
展开
-
C++文件流小程序
#include <iostream>using namespace std;void fun (char *s, int a, double f){ /**********found**********/ FILE * fp; char ch; fp = fopen("filel.txt", "w"); fprintf (fp, "%s%d%f\n", s原创 2016-08-29 23:26:36 · 464 阅读 · 0 评论 -
为什么C++不能有虚构造函数,却可以有虚析构函数
class B{ public : virtualvoid m1(); virtualvoid m2(); }; class D : public B{ virtualvoid m1(); } 一、C++的动态绑定使用vtable(虚成员函数表)来实现。vtable支持运行时查询,使系统可以将某一函数名绑定到vtable的特定入口地址。例如上段代码的虚函数表为:虚成员函数转载 2016-10-15 12:32:24 · 5869 阅读 · 0 评论 -
快速排序代码
#include <iostream>using namespace std;void Qsort(int a[], int low, int high);int main(){ int a[] = { 6, 1, 2, 7, 9, 3, 4, 5, 10, 8 }; cout << sizeof(a) << '\t' << sizeof(a[0]) << '\t' <<原创 2016-10-11 17:02:39 · 262 阅读 · 0 评论 -
堆排序算法代码
#include <iostream>#include <algorithm>using namespace std;void HeapAdjust(int *arr, int size, int element);void BuildHeap(int *arr, int size);void HeapSort(int *arr, int size);int main(){ int原创 2016-10-11 19:47:15 · 717 阅读 · 0 评论 -
C++ find函数
#include <iostream>#include <vector>#include <string>#include <algorithm> //find函数所在的头文件using namespace std;int main(){ string str = "you know how much i love you"; if(find(str.begin(),str原创 2016-09-21 23:51:56 · 731 阅读 · 0 评论 -
typedef和define具体的详细区别
1) #define是预处理指令,在编译预处理时进行简单的替换,不作正确性检查,不关含义是否正确照样带入,只有在编译已被展开的源程序时才会发现可能的错误并报错。例如:#define PI 3.1415926程序中的:area=PI*r*r 会替换为3.1415926*r*r 如果你把#define语句中的数字9 写成字母g 预处理也照样带入。2)typedef是在编译时处理的。它在自己的作用域内转载 2016-10-09 21:14:29 · 229 阅读 · 0 评论 -
struct、union的对齐问题
#include <stdio.h>/*32位机器访问起始地址是4的倍数处的内存比较方便 min(系统默认(4),数据成员自身长度)*/struct node1{ //sizeof(struct node1)=8; char a1; //1 short b1; //2 int c1; //4};struct node2{ //sizeof(struct n原创 2016-09-07 19:58:06 · 705 阅读 · 0 评论 -
有关数组的小处理问题
#include <iostream>using namespace std;int main(){ int arr[] = {1,2,3,4,5,6,7}; int len = 7; int *ptr = arr + 1; cout << ptr[0]<< ' ' <<ptr[len - 2] << endl; /*上面两条语句换成: arr ++原创 2016-09-23 00:25:28 · 258 阅读 · 0 评论 -
并差集的链表、路径压缩两种方法
Discription若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。 规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚。如果x,y是亲戚,那么x的亲戚都是y的亲戚,y的亲戚也都是x的亲戚。Input第一行:三个整数n,m,p,(n< =5000,m< =5000,p< =5000),分别表示有n个人,m个亲戚关系,询问p对亲转载 2016-09-05 14:25:15 · 290 阅读 · 0 评论 -
汉字字符处理
#include <iostream>#include <cstring> //strcmp、strcpy、strncpy函数所在头文件using namespace std;int main(){ char *str = "you爱我haishi他"; cout << "strlen(str)=" << strlen(str) <<" sizeof(str)=" << si原创 2016-09-11 22:25:38 · 381 阅读 · 0 评论 -
sort函数、max_element和min_element函数
#include <iostream>#include <string>#include <algorithm>using namespace std;int cmp(const char&a,const char& b){ if(a > b) return 1; else return 0;}int main(){ string s原创 2016-08-30 15:18:46 · 580 阅读 · 0 评论 -
利用O(1)空间复杂度对字符串排序(人人)
将字符串中字符按照“小写、大写、数字、符号”的顺序排列,要求利用O(1)的空间复杂度。 其中ASCII(小写)> ASCII(大写)> ASCII(数字) 符号要做特殊处理。 利用sort()函数,修改cmp(),进行排序。 #include <iostream>#include <algorithm>#include <string>using namespace st原创 2016-09-10 18:39:47 · 566 阅读 · 0 评论 -
实现a + b两个整数作和,不能用 '+' 号(人人网)
#include <iostream>using namespace std;int add(int first,int second);int main(){ int a,b; while(cin >> a >> b) { cout << add(a,b) << endl; } return 0;}int add(int a, int b)原创 2016-09-10 11:42:02 · 227 阅读 · 0 评论 -
define的理解
#include "stdio.h"#define a 10void foo();void prin();int main(){ prin(); printf("%d ", a); foo(); printf("%d ", a);}void foo(){#undef a#define a 50}void prin(){ printf("%d "原创 2016-10-06 23:35:58 · 311 阅读 · 0 评论