- 博客(39)
- 资源 (4)
- 收藏
- 关注
原创 cmake指定mingw编译器的方法
准备工作:先安装cmake和mingw g++编译器。一:新建一个测试工程,工程文件夹里面需要有两个文件: 分别是CMakeLists.txt和main.cpp。关于CMakeLists.txt怎么编写,可以查找cmake相关信息。二:CMD里先CD到工程目录下,然后运行命令:cmake.exe -G "MinGW Makefiles"三:运行命令:mingw32-m...
2019-05-31 11:38:30
9276
2
原创 singleton的实现代码
#include #include #include using namespace std;templateclass Singleton{public: static T* Instance() { if(ObjectPtr.get()==NULL) ObjectPtr.reset(new T()); retu
2014-04-15 15:23:17
893
1
原创 浮点数整数部分和小数部分的分离
void PrintFloat(float value){ int FloatValue = *reinterpret_cast(&value); if(FloatValue&0x80000000) //最高位为符号位 cout<<"-"<<endl; int Exp = (FloatValue>>23)&0xff; //取得指数字段,一个字节。第二到第
2013-04-19 14:54:17
12024
原创 关于二级指针
#include #include #include using namespace std;struct mst{ int *a;//对齐,扩展为8字节 double f; int *b; int *c;};int main(){ mst st; int array1[2]= {1,2}; int array2[3]= {3,4,5}; int array3[2]
2012-07-16 13:47:56
569
原创 关于二维指针的代码
#include #include using namespace std;struct mst{ int *a; double f; int *b; int *c;};int main(){ mst st; int array1[2]={1,2}; int array2[3]={3,4,5};
2012-07-15 14:59:20
460
原创 C++ builder 网络聊天程序
#include #pragma hdrstop#include "main.h"//---------------------------------------------------------------------------#pragma link "ScktComp"#pragma resource "*.dfm"TChatForm *ChatForm;
2012-07-02 15:54:53
1669
原创 大端模式和小端模式
大端格式:在这种格式中,字数据的高字节存储在低地址中,而字数据的低字节则存放在高地址中;小端格式:与大端存储格式相反,在小端存储格式中,低地址中存放的是字数据的低字节,高地址存放的是字数据的高字节。判断计算机系统是大端还是小端的代码:bool isBig_Endian(){ unsigned short test=0x1234; if(*((unsigned char*)
2012-04-19 19:00:45
980
原创 写着玩的代码
#include#include#includevoid output_empty(int n){ for(int i=0;i<n;i++) { printf(" "); }}int main(){ int i,j; for(int k=0;k<60;k++) { for(i=1;i<=5;i++) { output_empty(k); for(
2012-04-09 22:27:10
392
原创 MFC消息映射宏展开后代码
const AFX_MSGMAP* theClass::GetMessageMap() const { return GetThisMessageMap(); } const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap() { typedef theClass ThisClass; typedef baseC
2012-02-26 22:26:26
684
转载 C++指向类成员函数的指针
最近在开发中用到了函数指针,于是想整理了一下有关函数指针的概念。O(∩_∩)O~首先 函数指针是指向一组同类型的函数的指针;而类成员函数我们也可以相似的认为,它是指向同类中同一组类型的成员函数的指针,当然这里的成员函数更准确的讲应该是指非静态的成员函数。前者是直接指向函数地址的,而后者我们从字面上也可以知道 它肯定是跟类和对象有着关系的。函数指针实例:typedef int (*p)(
2012-02-17 16:49:17
432
原创 C++中实现C#属性相似的语法
#include#includeusing namespace std;templateResultFunction function_cast(Function fun){ ResultFunction result=ResultFunction(*(reinterpret_cast(&fun))); return result;}template//,ty
2012-02-17 00:38:33
824
原创 数组大小作为参数传递(通过模板数值参数)
#include#includeusing namespace std;templatevoid OutputArraySize(type (&ar)[size]){ cout<<"the array size is:"<<size<<endl;}int main(){ int ar[5]; double dous[10]; OutputArraySize(ar);
2012-02-14 13:54:29
967
原创 数值计算的符号转换和溢出
#include#includeusing namespace std;int main(){ unsigned short x=32; signed short y=-35; unsigned short z=x+y;//y根据singed short会被隐式转换为unsigned short,y会被转换成无符号数 cout<<z<<endl;//结果为65535+1-y+x。此
2012-02-10 12:37:50
880
转载 拷贝构造函数
拷贝构造函数是C++最基础的概念之一,大家自认为对拷贝构造函数了解么?请大家先回答一下三个问题:1. 以下函数哪个是拷贝构造函数,为什么?X::X(const X&); X::X(X); X::X(X&, int a=1); X::X(X&, int a=1, b=2); 2. 一个类中可以存在多于一个的拷贝构造函数吗?3. 写出以下程序段的输出结果,
2012-02-08 22:41:46
372
原创 mem_fun和bind2nd的用法例子
#include#include#includeusing namespace std;class Widget:public unary_function{ int id; public: Widget(int x):id(x){} void Multiply(int x) const {
2012-01-28 22:42:48
820
原创 C++中通过自定义operator new 实现内存分配跟踪
#include#includeusing namespace std;void* operator new(size_t size,const char *msg,int line){ cout<<"the file:"<<(char*)msg<<" line number:"<<line<<" and size is:"<<size<<endl; return mall
2012-01-22 20:31:33
1264
原创 auto_ptr预防作为STL容器元素的实现。
#include#include#include#includeusing namespace std;class Widget{ public: Widget(int _id):id(_id){} void SayHello() { cout<<"my id:"<<id<<endl; } ~
2012-01-21 00:03:36
421
原创 优化问题关于const第二篇
int main(){ const int maxint=100;//此段代码放到main函数外程序会运行时崩溃掉 int &msg=const_cast(maxint); msg*=200; print_value(maxint); cout<<"max:"<<maxint<<endl; return 0;}
2011-12-28 23:27:40
512
原创 类的引用类型成员
#include using namespace std;class Object{ int &count; public: Object(int &cou):count(cou){}; //如果函数为Object(int cou)则count引用了形参。 ~Object(){}; void SetValue(int cou) { count=cou
2011-11-24 11:35:09
1787
原创 MS C++的for each关键字
#include "stdafx.h"#include #include #include #include #include using namespace std;int _tmain(int argc, _TCHAR* argv[]){ std::
2011-07-20 08:19:01
711
原创 控制台颜色控制函数
SetConsoleTextAttribute(hOutput,Forg);是windows.h中的WINDOWS API函数。
2011-07-06 10:37:14
555
原创 大小写转换函数,位运算的使用
#include#include#include#includeusing namespace std;void ToUpper(char *str){ char *beg=str; char ch; for(int i=0,length=strlen(str);i='a'&&ch='A'&&ch
2011-04-27 09:46:00
755
原创 汇编写的内存拷贝函数
<br />void AsmCopyMemory(char *DESTION,char *SOURCE,unsigned int count){ int *des=(int*)DESTION; int *sou=(int*)SOURCE; count=count/4; _asm { MOV ECX,count MOV ESI,0L1: MOV EAX,[sou] MOV EBX,[des] MOV EDX,DWORD PTR [EAX] MOV
2011-04-26 11:08:00
3973
原创 通过函数名调用类的成员函数
<br />#include<iostream>#include<algorithm>#include<string>#include<vector>#include<map>using namespace std;class FuncFactory{ public: typedef void (FuncFactory::*Function)(string str); typedef void (*OutsideFun)();
2011-04-18 14:49:00
1393
原创 大学
大一时候呆的地方,很小,迷惘的一段时光啊,在学校网吧消费了300块钱,也努力学习了,上学期挂了4门,下学期忘了,可能挂一门。短短的几个月经历了高潮与低谷后明白了,对荣誉太过关注会让自己很受伤,从那以后把60分定为自己的目标,同时确立了要成为一名优秀的程序员的目标。大二时才建起的学院楼,每次进去都会很难过,不是学费就是学习出问题了,反正没好事。还怕在这里做实验。作为英语角的时候从没去过(就这水平,哪敢去啊,哪个MM要跟我对话那不得憋死)。不过偶尔一个人在这里看书(多数时间是不想学习,想在这看风景)。从这里经过
2011-03-04 22:37:00
625
2
原创 计算字符串的最小后继
<br />#include <stdio.h>#include <iostream>#include <string>#include <algorithm>using namespace std;string GetString(string str){ int left=1; string result; for(int i=str.length()-1;i>=0;--i) { if((char)(str[i]+left)>'z') { re
2010-12-31 11:59:00
1052
原创 迭代和递归计算分数
<br />#include<iostream>#include<algorithm>#include<string>#include<cstring>#include<ctime>#include<windows.h>using namespace std;struct CopyStruct{ double a; double b;};void StructCopyMemory(void *destion,void *source,int count
2010-12-31 11:37:00
515
原创 模仿虚函数
<br />#include<iostream>#include<algorithm>#include<string>using namespace std;class Base{ public: int funptr; void (Base::*Show)(); Base(){Show=&Base::Display;} ~Base(){}; void Display() { typedef void (*Function)(const
2010-12-03 11:42:00
322
原创 .*操作符的用法
<br /> <br />#include <iostream>#include <algorithm>#include <vector>#include <string>using namespace std;class Widget{ static int counter;public: Widget() { counter++; cout<<"my id is:"<<counter<<endl;
2010-11-23 13:27:00
576
原创 模仿mem_fun函数的功能
<br /> <br />#include <iostream>#include <algorithm>#include <vector>#include <string>using namespace std;class Widget{ static int counter;public: Widget() { counter++; cout<<"my id is:"<<counter<<endl;
2010-11-23 13:17:00
457
原创 内存拷贝函数不同写法的效率分析
<br />#include<iostream>#include<algorithm>#include<string>using namespace std;void OverFlow(const int &max){ int &tmp=const_cast<int&>(max); tmp=200; cout<<"the value of tmp"<<tmp<<endl;//输出200 cout<<"max in the function"<<ma
2010-11-23 10:19:00
875
原创 优化问题关于const
<br />#include<iostream>#include<algorithm>#include<string>using namespace std;void OverFlow(const int &max){ int &tmp=const_cast<int&>(max); tmp=200; cout<<"the value of tmp"<<tmp<<endl;//输出200 cout<<"max in the function"<<ma
2010-11-09 14:36:00
431
原创 实现一个功能类似Any的类
<br />#include<iostream>#include<algorithm>#include<string>#include<stdexcept>using namespace std;enum Type{Int,Dou,Str};class Any{ private: void *ptr; Type type; public: Any(); ~Any(); Any operator=(int x);
2010-11-09 09:50:00
442
原创 成员函数转换普通函数
#include#includeusing namespace std;class Widget{ string name;public: Widget(string _name):name(_name) { } ~Widget() { //cout<<"goodbye:"<<name<<endl; } void Show(string &str) { cout
2010-11-09 09:47:00
1009
原创 调用容器内对象成员函数
<br />#include<boost/lexical_cast.hpp>#include<vector>using namespace std;using namespace boost;class Widget{ string name; public: Widget(int x) { name=lexical_cast<string>(x); } ~Widget() { } void ShowValue() {
2010-11-04 08:57:00
761
原创 通过vtable指针访问声明为private的虚函数
<br />class Base{private: virtual void f() { cout << "Base::f" << endl; } virtual void g() { cout << "Base::g" << endl; } virtual void h() { cout << "Base::h" << endl; }};class
2010-11-04 08:47:00
555
原创 智能指针与STL容器
<br />#include "boost/shared_ptr.hpp"#include <vector>#include <iostream>class A{public: virtual void sing() { std::cout <<"A::sing " <<number<<std::endl; } A() { number=counter+1; counter++;
2010-11-04 08:44:00
2137
原创 实现一个可以用于bind2nd的函数对象
template<typename T>class Mulitiplies:public binary_function<T,T,T>{ public: Mulitiplies(){} ~Mulitiplies(){} T operator()(T x,T y) const { return x*y*3.14159265; }};int main(){ vector<double> vecs_int; for(dou
2010-11-04 08:41:00
525
原创 李清照词
点绛唇 寂寞深闺,柔肠一寸愁千缕。惜春春去,几点催花雨。 倚遍栏干,只是无情绪!人何处?连天衰草,望断归来路。 点绛唇 蹴罢秋千,起来慵整纤纤手。露浓花瘦,薄汗轻衣透。 见有人来,袜铲金钗溜,和羞走。倚门回首,却把青梅嗅。 (此首一作无名氏词,
2009-10-23 19:19:00
1210
1
C++源码300例-解决很多c++的学习问题
2010-03-09
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人