
c/c++
Xia__Quan
这个作者很懒,什么都没留下…
展开
-
STL标准算法(二)排序算法和二分法搜索以及合并算法
排序Sorting:sort 按照某种条件,或者按正常 排序stable_sort 正常跑徐partial_sort 局部排序partial_sort_copy 局部排序nth_element 保证n在第n位,比它大的在后面,比它小的在前面,但不能保证是有序的二分法搜索Binary search (operating on sorted ranges):原创 2017-05-15 14:46:27 · 314 阅读 · 0 评论 -
C++类的对象和类对象指针
class Student { public: static int number; string name; public: Student() { } }; int main(int argc, char** argv) { Student* s1; s1 = new St原创 2017-05-19 17:54:38 · 341 阅读 · 0 评论 -
C++ 友元函数
#include "stdlib.h"#include "stdio.h" class a{private: char c = '123'; friend void Print(const a obj); }; void Print(const a obj) { printf("%c", obj.c); }void main() { a obj; Pr原创 2017-05-22 15:26:25 · 230 阅读 · 0 评论 -
C++ 智能指针
C++ 智能指针详解 一、简介由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete。程序员忘记 delete,流程太复杂,最终导致没有 delete,异常导致程序过早退出,没有执行 delete 的情况并不罕见。用智能指针便可以有效缓解这类问题,本文主要讲解参见的智能指针的用法。包括:std::auto_ptr、boost::scoped_p转载 2017-05-22 15:59:35 · 266 阅读 · 0 评论 -
C++ 二级指针
void GetMemory(int *p, int num){*p = 2;}void main() {int a = 1;int* ptr = &a;//一级指针int** ptr01 = &ptr;//二级指针 指向一级指针所在地址printf("%d\n", ptr);printf("%d", *ptr01);GetMemory(ptr, 100);原创 2017-05-22 17:47:12 · 412 阅读 · 0 评论 -
C++快速排序、归并排序
归并排序void Merge(int *a, int p, int q, int r) { int n1 = q-p+1; int n2 = r-q; int *L = new int[n1+1]; int *R = new int[n2+1]; int i, j, k; for (i=0转载 2017-06-22 17:53:45 · 315 阅读 · 0 评论 -
C++ Vector容器
#include #include #include #include #include using namespace std; void myfunction(int i) { cout " " i;}void main() { vectorint> myvector; vectorint> myvector01; myvector.push_back(1原创 2017-06-27 17:51:06 · 196 阅读 · 0 评论 -
C++ printf
原创 2017-10-30 15:18:02 · 930 阅读 · 0 评论 -
递归
fibonacci序列 斐波那契数列(Fibonacci sequence)int fib(int n) { if(n==1) return (0); else if(n==2) return (1); else return (fib(n-2)+fib(n-1)); }原创 2017-06-23 09:24:08 · 195 阅读 · 0 评论 -
c++ *和++同时作用变量
total +=*start++;(start是指向数组的指针)*和++有同样的优先级 但它在结合时是从右向左进行的,这就意味着++是应用于start的而不是*start。也就是说是指针自增1,而不是指针所指向的数据自增1 ,即先把指针指向的数据加到total上然后指针在自增1,为了清晰可见 还是使用 *(strat)++.(如果程序是*++start,则变成了指针先自增1,然后再使用其所原创 2017-10-30 17:41:45 · 336 阅读 · 0 评论 -
c++ const修饰指针
#include #define MONTH 12void main() { const int days[MONTH] = {11,22,33,44,55,55,66,77,88,88,99,99}; //days[4] = 11;编译错误 指向常亮的指针不能用于修改数值 double rates[5] = {88.99,100.12,59.45,183.11,11.11};原创 2017-10-31 10:24:12 · 231 阅读 · 0 评论 -
c++ 字符串
//初始化一个大小已确定的char数组 const char m1[40] = "Limit yourself to one line's worth."; //让编译器计算数组大小 const char m2[] = "If you can't think of anything,fake it."; //初始化一个指针 const char *m3 = "\nEnough about原创 2017-10-31 14:26:19 · 201 阅读 · 0 评论 -
C++ static
static1、作用域使用static修饰的全局变量能够在各个函数内部被使用,但数不能在文件的外部被使用2、类里面static修饰变量不能初始化class a{public:static int num;}int a::num =0;3、如果static在类里面,可以用于类的计数原创 2017-05-12 10:46:39 · 266 阅读 · 0 评论 -
c++ malloc
#include #include#include void main() { double* ptd; int max; int number; int i = 0; ptd = (double *)malloc(30*sizeof(double)); long*newmem; newmem = (long*)calloc(100,sizeof(long));原创 2017-10-31 15:55:10 · 547 阅读 · 0 评论 -
c++ 二维数组
int zippo[4][2] = { {1,2},{2,3},{3,4},{4,5} };int(*pz)[2];pz = zippo;pz++;getchar();原创 2017-10-31 11:01:38 · 320 阅读 · 0 评论 -
c++函数和指针
void(*pf)(char *);//pf是一个指向函数的指针void* pf(char *);//返回一个指针的函数原创 2017-10-31 17:09:53 · 194 阅读 · 0 评论 -
STL标准算法(三)其它算法
Heap:push_heap pop_heap make_heap sort_heap 排序Min/max:min 得到最小值 max 得到最大值 min_element 得到最小值 max_element 得到最大值 lexicographical_compare 字母个数比较next_得到排列组合prev_permutation 得原创 2017-05-15 14:47:25 · 224 阅读 · 0 评论 -
C++ 引用 左值引用 右值引用
c语言没有引用c++有引用引用的产生:如果改变一个一级指针,需要一个二级指针,改变一个二级指针需要一个三级指针,比较麻烦,就出来了引用右值:在寄存器内,无法去地址左值:可以&右值引用&& 只能绑定在一个左值上(有一个例外)左值引用 & 只能绑定在一个右值上(有一个例外)左值引用int a=1;int & aa =a;//左值引用原创 2017-05-12 10:27:31 · 322 阅读 · 0 评论 -
C++ 指针
1、在32位平台,指针占4个字节,指针本身就是地址int a =1;int * p =&a;//一级指针int **p =&p; //二级指针2、指针运算int aa[3] = { 1,2,3 };int* pp = aa;pp++;实际上加了int的数据类型个字节那么pp++后的*pp值为23、指针函数 函数指针函数指针 int (*add)(int原创 2017-05-12 14:14:50 · 177 阅读 · 0 评论 -
C++ inline
1、和define很像,但是define类型不匹配也能通过,inline必须类型匹配,= inline和define还有一点很像,就是不用每一个函数都去打开,开始的时候就打开好了,那样速度相比普通函数更快2、inline的定义放在头文件中3、inline对编译器只是建议一般情况下,对内联函数有如下建议1、不能有递归2、不能包含静态数据3、不能包含循环原创 2017-05-12 11:01:51 · 178 阅读 · 0 评论 -
C++ union
1、成员都是公有,但不能修改2、union myunion{ int num; double db; void go{} }占8个字节,因为内存共享原创 2017-05-12 11:04:46 · 210 阅读 · 0 评论 -
C++ explict
explicit 关键字只能用于类内部的构造函数声明上。explicit 关键字作用于单个参数的构造函数。在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换。class Circle { public: explicit Circle(double r) : R(r) {} }; 未加explic原创 2017-05-12 11:11:34 · 389 阅读 · 0 评论 -
C++ Virtial
虚函数实际上就是指针 占四个字节虚函数 优先从子类寻找虚函数声明:virtual 函数类型 函数名(参数列表)=01 #include 2 using namespace std; 3 class Base{ 4 public: 5 void m() 6 { 7 cout "it's Base's m() " end转载 2017-05-12 11:24:36 · 319 阅读 · 0 评论 -
C++ const
1、定义常量(1)const修饰变量,以下两种定义形式在本质上是一样的。它的含义是:const修饰的类型为TYPE的变量value是不可变的。TYPE const ValueName = value; const TYPE ValueName = value;(2)将const改为外部连接,作用于扩大至全局,编译时会分配内存,并且可以不进行初始化原创 2017-05-12 11:36:48 · 168 阅读 · 0 评论 -
C++ namespace
namespace的由来:c文件中如果有两个文件同时定义了int i=0;那么编译不了,出现重定义错误因为命名空间的区别,导致c++的库都没有.h结尾,以此区分1、命名空间可以赋值#include#include#includenamespace haha{int a =10;}namespace xixi{int aaa =15;} ;namesp原创 2017-05-12 14:22:40 · 238 阅读 · 0 评论 -
C++ 函数模板
template inline T const& max(T const& a, T const& b) { return a } 对于上面的模板函数,我们在实际的应用中可以用任意类型来实例化该模板函数,如: int main() { printf("The max value is %d\n", ::max(4,5)原创 2017-05-12 14:33:46 · 199 阅读 · 0 评论 -
C++结构体和C++ 类和C结构体比较
c结构体和c++结构体c结构体只能定义变量,不能定义函数,可以定义函数指针c++结构体,可以包含函数,能够定义private public protectd,可以从别的类继承,也可以被继承,可以有虚函数,体现了数据结构和算法的结合c结构体声明struct的时候,必须加上struct a aa;c++可以忽略 a aa;c++结构体和c++类的区别对于成员访问原创 2017-05-12 14:46:19 · 1260 阅读 · 0 评论 -
平衡二叉树(AVL树)
#include "stdlib.h"#include "stdio.h"#include using namespace std;typedef int data_type;typedef struct bst_node{ data_type data; struct bst_node * lchild, *rchild; }bst_t,*bst_p; static bst原创 2017-05-23 14:58:53 · 215 阅读 · 0 评论 -
LinkList链表
#includeusing namespace std;struct Bean { string str; int id;}; struct Node {//需要有下一个节点的指针,需要一个bean存储这个节点的数据 Node* next; Bean bean; Node(Bean b) :bean(b),next(NULL) {} }; class LinkLis原创 2017-05-23 18:32:56 · 334 阅读 · 0 评论 -
C++ typedef
typedef提示#define AREA double 与typedef double AREA 可以达到相同的效果。但实质不同,#define为预编译处理命令,主要定义常量,此常量可以为任何的字符及其组合,在编译之前,将此常量出现的所有位置,用其代表的字符或字符组合无条件的替换,然后进行编译。typedef是为已知数据类型增加一个新名称typedef struct{原创 2017-05-12 10:54:18 · 380 阅读 · 0 评论 -
C++ 选择排序、冒泡排序、插入排序
//sort.h#include "stdlib.h"#include "stdio.h"#include templatetypename T>class Sort { public: //void SelectSort(T* array, int size); void SelectSort(T*array, int size) { int temp原创 2017-05-24 11:23:38 · 277 阅读 · 0 评论 -
STL标准算法(一)序列查找算法和序列修改算法
一、序列算法Non-modifying sequence operations:for_each 遍历输出参考C++函数手册.chm http://download.youkuaiyun.com/detail/u013866845/9841336find 查找一个元素在序列中是否存在find_if 查找一个符合条件的元素在序列中是否存在,find_end 一个序列的某个元素原创 2017-05-13 11:42:12 · 418 阅读 · 0 评论 -
二进制、八进制、十进制、十六进制
二进制转十进制二进制1101 1x2的3次方+1x2的二次方+1x2的1次方+1x2的0次方转换为十进制 1x8+1x4+0x2+1x1=13原创 2017-10-31 17:19:12 · 433 阅读 · 0 评论