随笔分类 -  C++

摘要:#include <iostream>using namespace std;#define Maxsize 10template <typename T>class queue{ private: int front,rear; T elem[Maxsize]; public: bool init(); bool QueueEmpty(); bool EnQueue(T e); T GetFront(); T DeQueue();};template <typename T>bool queue<T>::init(){ front = rear 阅读全文
posted @ 2012-10-26 13:44 SA高处不胜寒 阅读(140) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;void print(int a[], int n){ for(int j= 0; j<n; j++){ cout<<a[j] <<" "; } cout<<endl; }int partition(int a[], int low, int high){ int privotval = a[low]; while (low < high) { while (low < high & privotval < a[hig 阅读全文
posted @ 2012-10-23 22:20 SA高处不胜寒 阅读(149) 评论(0) 推荐(0)
摘要:我有一个程序是这样的:int main(){ int *p; int i; int*fun(void); p=fun(); for(i=0;i<3;i++) { printf("%d\n",*p); p++; } return 0;};int* fun(void){ static int str[]={1,2,3,4,5}; int*q=str; return q;}我想问一下,除了将str定义为静态区以及用malloc这样的方法外,还有什么好的方法,同时也想问一下如果我改成int main(){ char *p; char*fun(void); p=fun(); p 阅读全文
posted @ 2012-10-22 09:19 SA高处不胜寒 阅读(723) 评论(0) 推荐(1)
摘要:#include <iostream>#include <vector>#include <fstream>using namespace std;int main(void){ ifstream fp("I:\\temp.txt"); vector< vector<double> > tmp; // string str; char str[100]; double i; char *ch,*ch1; int num; while(!fp.eof()) { vector<double> m1; // 阅读全文
posted @ 2012-10-22 08:52 SA高处不胜寒 阅读(758) 评论(0) 推荐(0)
摘要:在类中如果需要其他对象作为成员函数时,需要注意以下几点:1、 类中如果需要其他对象作为私有变量或者protected时,只能调用没有参数的构造函数例如:#include <iostream>using namespace std;class A{public: A(char *p) { des = new char[10]; strcpy(des, p); cout<<"OK2"<<endl; } A(){cout<<"BOK"<<endl;} void show() { cout<< 阅读全文
posted @ 2012-10-20 20:52 SA高处不胜寒 阅读(857) 评论(0) 推荐(0)
摘要:下面是我摘录的网上的解释:有如下的两个结构体:struct A struct B { {int a; int a;unsigned __int64 b; short c; short c; unsigned __int64 b;}; }; 那么 sizeof(A) 和 sizeof(B) 一样吗?让我在编译器里试一下,啊 ? 怎么不一样?两个结构体明明相同,只是第二和第三个成员变量的位置颠倒了结果却大相径庭。到底是因为什么呢?答案是编译器的数据对齐方式在作怪。以 vc6.0 为例,默认情况下的对其方式是 8 位。所以 struct A 的大小为 24 , struct B 的大小为 16, 下 阅读全文
posted @ 2012-10-18 21:47 SA高处不胜寒 阅读(1531) 评论(0) 推荐(0)
摘要:file1.c其中变量及函数声明时需要extern 但是在定义时不能添加,如下所示,file1.c:其中若file1.c中取消注释,则编译通过,因为对于static它是仅局限于静态链接的,只在本文件内使用,而外部文件不能使用。 阅读全文
posted @ 2012-10-17 23:11 SA高处不胜寒 阅读(194) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;char* strcatcp(char *des, char *source1, char *source2){ char *p = des; while ((*des++=*source1++)!='\0'); des--; while ((*des++=*source2++)!='\0'); return p;} int main(void){ char *des, *source1, *source2; des = (char *)malloc(sizeof(sour 阅读全文
posted @ 2012-10-17 09:58 SA高处不胜寒 阅读(636) 评论(0) 推荐(0)
摘要:C++2.0以后全面支持虚函数与虚继承,这两个特性的引入为C++增强了不少功能,也引入了不少烦恼。虚函数与虚继承有哪些特性,今天就不记录 了,如果能搞了解一下编译器是如何实现虚函数和虚继承,它们在类的内存空间中又是如何布局的,却可以对C++的了解深入不少。这段时间花了一些时间了解这 些玩意,搞得偶都,不过总算有些收获,嘿嘿。先看一段代码class A{ virtual aa(){};};class B : public virtual A{ char j[3]; //加入一个变量是为了看清楚class中的vfptr放在什么位置 public: virtual bb(){};};class C 阅读全文
posted @ 2012-10-16 09:51 SA高处不胜寒 阅读(219) 评论(0) 推荐(0)
摘要:#include <iostream>#include <sstream>#include <stdio.h>#include <math.h>using namespace std;char ch[20];int cnt = 0;int split_int(int num){ int temp1,temp2; char ch1[6]; int i=0; while(num%10) { temp1 = num%10; ch1[i] = temp1+'0'; i++; num=num/10; } for(int k=i-1;k> 阅读全文
posted @ 2012-09-12 19:35 SA高处不胜寒 阅读(4883) 评论(0) 推荐(0)
摘要:编写函数,统计在某段英文文本完整句子的数目,文本只包括大小写英文字母,空格,点号(.),逗号(,),完整句子必须包含至少一个字母并以一个点号结束。实现了一下,不知道可有漏洞#include <iostream>#include <fstream>using namespace std;#define maxsize 1024int get_sentence_num(){ fstream file1("I:\\origin.txt"); char ch,des_ch[maxsize]; int flag = 0, i = 0,result=0; whi 阅读全文
posted @ 2012-09-11 22:10 SA高处不胜寒 阅读(242) 评论(0) 推荐(0)
摘要:下面所指的signal都是指以前的older signal函数,现在大多系统都用sigaction重新实现了signal函数1、signal在调用handler之前先把信号的handler指针恢复;sigaction调用之后不会恢复handler指针,直到再次调用sigaction修改handler指针。:这样,(1)signal就会丢失信号,而且不能处理重复的信号,而sigaction就可以。因为signal在得到信号和调用handler之间有个时间把handler恢复了,这样再次接收到此信号就会执行默认的handler。(虽然有些调用,在handler的以开头再次置handler,这样只能 阅读全文
posted @ 2012-09-10 18:54 SA高处不胜寒 阅读(8275) 评论(2) 推荐(1)
摘要:#include <iostream>using namespace std;class a{public:virtual void aa(){};};class b:public a{public:virtual void aa(){};virtual void bb(){};};class c: public b{public:virtual void aa(){};virtual void cc(){};};int main(){cout<<sizeof(a)<<endl;cout<<sizeof(b)<<endl;cout&l 阅读全文
posted @ 2012-09-10 15:30 SA高处不胜寒 阅读(503) 评论(0) 推荐(0)
摘要:首先我们来看一段代码#include <iostream>using namespace std;class example{public: example() { output(); } virtual void output() { cout<<"The construct can call virtual function!"<<endl; }};class exam:public example{ public: virtual void output() { cout<<"The second one!&q 阅读全文
posted @ 2012-09-10 15:24 SA高处不胜寒 阅读(1828) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;void DecToBin(unsigned int dec,char ch[] ){ int i = 31; while(i >= 0) { int temp = dec; temp = temp >> i; ch[32-i-1] = (temp & 1)+'0'; i--; }}int main(){ char ch1[33]; DecToBin(12,ch1); ch1[32] = '\0'; cout<<ch1<<e 阅读全文
posted @ 2012-08-22 22:07 SA高处不胜寒 阅读(1814) 评论(0) 推荐(0)
摘要:今天好友微软笔试,把题目发给我看了,不过由于英语不好,看了很久才懂,唉,题目如下:Here goes the question: > > Write a method that find the int that has the most occurred number 1 in the input int array, and write test cases that you can think.> > Please return me the answer no later than 20:20.这上面的1当时理解为整形的二进制中的1,则代码如下:#include 阅读全文
posted @ 2012-08-22 21:42 SA高处不胜寒 阅读(175) 评论(0) 推荐(0)
摘要:const修饰符可以把对象转变成常数对象,什么意思呢?意思就是说利用const进行修饰的变量的值在程序的任意位置将不能再被修改,就如同常数一样使用! 使用方法是:const int a=1;//这里定义了一个int类型的const常数变量a; 但就于指针来说const仍然是起作用的,以下有两点要十分注意,因为下面的两个问题很容易混淆! 我们来看一个如下的例子:#include <iostream>using namespace std;void main(void){const int a=10;int b=20;const int *pi;pi=&a;cout <& 阅读全文
posted @ 2012-08-22 15:51 SA高处不胜寒 阅读(180) 评论(0) 推荐(0)
摘要:#include <vector>#include <iterator>#include <iostream>using namespace std; void printMy(vector<int>); int main(){ vector<int> vecInt; vecInt.push_back (1); vecInt.push_back (6); vecInt.push_back (6); vecInt.push_back (3); vector<int>::iterator itor; vector<int 阅读全文
posted @ 2012-08-22 15:39 SA高处不胜寒 阅读(567) 评论(0) 推荐(0)
摘要:今天尝试在类中实现结构体,但是在类中对结构体进行声明实现,然后对其进行实现就会报错,例如按照如下的实现方式#include <iostream>using namespace std;class A{public: struct one{ string name; int age; }obj; void shuchu();};void shuchu(){ cout<<"a"<<endl;}int main(){ A a; a.obj = {"chenhongliang",1}; return 0;}这样会编译不过,但该 阅读全文
posted @ 2012-08-20 23:06 SA高处不胜寒 阅读(534) 评论(0) 推荐(0)
摘要:vector中如果元素是struct类型或者class类型,若要对其中某个类型的元素进行排序,具体代码的编写如下所示:#include <iostream>#include <vector>using namespace std;struct Record{ string name; char add[24];};struct Cmp_by_name //为排序编写的结构体{ bool operator()(const Record &a, const Record &b) { return a.name < b.name; }};int main 阅读全文
posted @ 2012-08-19 19:19 SA高处不胜寒 阅读(1236) 评论(0) 推荐(0)