![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++
wae_gossip
这个作者很懒,什么都没留下…
展开
-
C++动态数组分配
在c++中时不能像java中一样定义这样的数组 int[] arr = new int[],如果我们要根据程序的运行来实现动态数组在c++中利用指针这个好东东。动态一维数组:#includeusing namespace std;int main(){ int len; cin>>len; int *p=new int[len];转载 2012-05-11 09:56:34 · 279 阅读 · 0 评论 -
C++类初始化,继承等相关易错点
很少编类,今晚试了试手,从下面的代码得到了几点经验:1、成员函数如果不是构造函数则需要指定返回值(或者void)2、类的成员变量在继承的时候,并不是单独的覆盖,而是有点像局部变量,和父类的同名变量可共存,从代码编译结果可以知道,两个变量的存储地址是不一样的。#includeclass Base{public: int a; Base() { a=0; co原创 2012-08-06 22:37:22 · 533 阅读 · 0 评论 -
友元类的相关知识
1、友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)。 2、友元关系不能被继承。 3、友元关系是单向的,不具有交换性。若类B是类A的友元,类A不一定是类B的友元,要看在类中是否有相应的声明。 4、友元关系不具有传递性。若类B是类A的友元,类C是B的友元,类C不一定是类A的友元,同样要看类中是否有相应的申明。#i原创 2012-08-09 17:58:41 · 2044 阅读 · 0 评论 -
构造函数初始化列表相关知识点集合
1、构造函数初始化列表仅用于初始化成员的值,并不指定这些初始化执行的次序。成员被初始化的次序就是定义成员的次序。第一个被定义的成员先被初始化,依次类推。一般,初始化的顺序无关紧要,然而,如果一个成员是根据其他成员而初始化,则成员的初始化顺序是至关重要的2、当成员变量是const 类型的时候,只能使用初始化列表初始化,在构造函数里面赋值是会出错的。常量变量只能被初始化,所以必须在初始化列表中完成原创 2012-08-09 17:36:09 · 374 阅读 · 0 评论 -
十进制转N进制的实现
#includeint Check(int i,int N){ while(i>0) { int k=i%10; if(k>=N) { cout<<"ERROR!"<<endl; return 1; } i=i/10; } return 0;}int Calc(int i,int N){ int k=i/10*N原创 2012-08-13 16:19:29 · 1025 阅读 · 0 评论 -
一道面试题“传入一个12345,返回结果54321”的实现
#includeint check(int a,int &b){ if(a==0) { return 0; } int k=a; int i=1; int gewei; gewei=a%10; while(k!=0) { k=k/10; i=i*10; } b=b+gewei*i/10; check(a/10,b);}main(){原创 2012-08-20 17:26:46 · 959 阅读 · 0 评论 -
联合体相关知识点
联合体和类不同,类里面的元素是占用各自的内存空间,而联合体是共用一段内存空间,而该内存空间存储的是该联合体里面最大的数。#includeunion{ int a; struct{ char i; char j; }k;}u;main(){ u.a=0x4142; cout<<hex<<u.a<<endl; cout<<dec<<u.k.i<<endl; cout原创 2012-08-13 15:11:40 · 289 阅读 · 0 评论 -
赋值构造函数和拷贝构造函数
#include "stdafx.h" #include #include using namespace std;class String { char * m_data;public: String (const char * other) { cout<<"我是默认构造函数"<<endl; int len; len=strlen(other)+1; m_d原创 2012-09-02 11:37:31 · 395 阅读 · 1 评论 -
类构造和析构顺序
#include "stdafx.h"#include using namespace std;class A{public: A() { cout<<"I am A"<<endl; } ~A() { cout<<"I am A destory"<<endl; }};class B{ public: B() { cout<<"I am B"<<endl;原创 2012-09-16 10:40:48 · 350 阅读 · 0 评论 -
protected 成员值得注意的易错点
#include "stdafx.h"#include #include using namespace std;struct A{protected: int num;public: A() { cout<<"AAA"<<endl; }};struct B: A{public: int test(A &a,B &b) { int qq=num; qq原创 2012-09-23 11:19:55 · 361 阅读 · 0 评论 -
各种继承关系
#include "stdafx.h"#include #include using namespace std;struct A{protected: int num;public: A() { cout<<"AAA"<<endl; foo(); } ~A() { cout<<"AAA~~"<<endl; } int virtual foo() {原创 2012-10-13 00:43:33 · 300 阅读 · 0 评论 -
动态继承
# include using namespace std;struct A{ int a; A() { cout<<"I am A"<<endl; a=1; } ~A() { cout<<"I am A destruct"<<endl; }};struct B:public A{ B() { cout<<"I am B"<<endl; a=0;原创 2012-10-16 21:17:53 · 478 阅读 · 0 评论 -
GetMemory(),运行Test 函数会有什么样的结果?
题目:void GetMemory(char *p){p = (char *)malloc(100);}void Test(void){char *str = NULL;GetMemory(str);strcpy(str, "hello world");printf(str);}请问运行Test 函数会有什么样的结果?分析:程序崩溃。因为GetMem转载 2012-10-23 13:58:38 · 6956 阅读 · 0 评论 -
求一个无符号整形变量1的个数
第一种方法:位操作,这种操作的复杂度是二进制的位数:#includemain(){ unsigned int a; cin>>a; int count=0; int num=0; while(count<8) { if(a&1==1) { num++; } a>>=1; count++; } cout<<num<<endl;原创 2012-07-13 09:52:23 · 537 阅读 · 0 评论 -
N个数中前K个小的树(快排的改进)
在没有改进快排实现得到K个数时,时间复杂度达到了O(NlogN),而改进之后时间复杂度降到了O(NlogK).#includeint Solt(int *a,int start,int end,int k){ if(start==end) { return 0; } int Post=start; int Temp=a[start]; int Change;原创 2012-06-28 20:36:53 · 813 阅读 · 0 评论 -
程序圧栈的顺序
最近看前人写了一篇文章,是关于堆栈的相关知识,里面讲到了程序圧栈的顺序,很是经典,现转贴原帖,顺,膜拜之:堆和栈的区别一、预备知识—程序的内存分配一个由c/C++编译的程序占用的内存分为以下几个部分1、栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。2、堆区(heap) — 一般由程序员分配释放, 若程转载 2012-06-28 10:49:24 · 507 阅读 · 0 评论 -
交换两个数据的值而不采用新的存储单位(C++实现)
#include void Swap(int &a, int &b){ a^=b; b^=a; a^=b;}main(){ int a; int b;cout<<"交换两个数据而不采用新的存储单位"<<endl;cin>>a;cin>>b;cout<<" a is "<<a<<endl;cout<<" b is "<<b<<endl;Swap(a原创 2012-05-15 10:22:38 · 287 阅读 · 0 评论 -
C++实现堆排序(更新版)
#includevoid Exchange(int *a,int i,int largest) {int temp=a[i];a[i]=a[largest];a[largest]=temp; } Keep(int *a,int i,int n) { int largest; int l=2*i+1;int r=2*i+2; if(la[i]) {原创 2012-06-11 20:29:59 · 297 阅读 · 0 评论 -
链表的创建,插入,删除
#includestruct Node{int num;Node *next;};Node *Creat(){ int n=0; Node * p1,*p2,*head; //Node *p1,p2,head和这个声明不同 head=NULL; p1=new Node; p2=new Node; head=new Node; head->next=p2; cou原创 2012-06-14 22:01:41 · 258 阅读 · 0 评论 -
浅析C++里的new
new运算符用于动态分配内存new用法: 1. 开辟单变量地址空间 1)new int; //开辟一个存放数组的存储空间,返回一个指向该存储空间的地址.int *a = new int 即为将一个int类型的地址赋值给整型指针a. 2)int *a = new int(5) 作原创 2012-06-14 20:15:46 · 352 阅读 · 0 评论 -
链表的创建,插入,删除,逆序
今晚折腾了一晚,发现还是临界条件没设置好,下面是更新版的:#includestruct Node{int num;Node *next;Node *LChild;Node *RChild;};Node *Creat(){ int n=0; Node * p1,*p2,*head; //Node *p1,p2,head和这个声明不同 head=NULL; p1=n原创 2012-06-18 20:50:10 · 292 阅读 · 0 评论 -
初窥this指针
1.何谓this指针 C++primer里定义this指针:指向该类对象的一个指针。成员还是不能定义this指针,但是成员函数可以显式调用this指针。2.何时用this指针 尽管在成员函数内部显式调用this指针通常是没必要的。但是有一种情况我们是必须这么做:当我们需要将一个对象作为整体引用而不是引用对象的一个成员。所以,在该函数返回对调用该函数的对象的引用时需要th原创 2012-06-20 22:12:01 · 805 阅读 · 0 评论 -
C++实现最优二叉树(修改版)
#includestruct Node{int num;Node *next;Node *LChild;Node *RChild;};void Print(Node *a) { Node *p=a->next; while(p!=NULL) { coutnum<<endl; p=p->next;原创 2012-06-19 23:31:12 · 1325 阅读 · 0 评论 -
C++实现二叉树(初始化,删除,遍历)
#includestruct Node{int num;Node *LChild;Node *RChild;Node *Father;};int Init(Node *a){ cout<<"Plea Input The Num"<<endl; cin>>a->num; if(a->num==0) { if(a==a->Father->LChild) {原创 2012-06-20 21:03:35 · 4900 阅读 · 1 评论 -
民生银行2013年暑期实习生笔试编程题
题目是:求一个数N的阶乘末尾含有多少个0解题思想:末尾0是由2*5的来的,所以考虑因式分解,得到有多少个2或者5,然后,对于一个数来说,被2整除的个数肯定比5多,所以这里只考虑被5整除的个数。#includemain(){ int a; cout<<"input the num you want to count"<<endl; cin>>a; int temp=1;原创 2012-06-24 10:05:13 · 2177 阅读 · 0 评论 -
C++实现栈
这是从前一篇博客的"C++实现队列"稍作修改得到的:#includestruct Node{ int num; Node *next; Node *father;};class quene{Node *tail;Node *head;public: quene(); //构造函数不能有返回值 pop(); push(); print(); c原创 2012-06-24 12:36:23 · 382 阅读 · 0 评论 -
C++实现队列
#includestruct Node{ int num; Node *next; Node *father;};class quene{Node *tail;Node *head;public: quene(); //构造函数不能有返回值 pop(); push(); print(); count();}; quene::quene()原创 2012-06-24 12:25:37 · 870 阅读 · 0 评论 -
局部变量和全局变量的相关问题
#include #include using namespace std;main(){char str1[]="hello world";char str2[]="hello world";char *str3="hello world";char *str4="hello world";if(str1==str2) cout<<"str1 and str2 is原创 2012-10-23 21:09:16 · 662 阅读 · 0 评论