12,827
社区成员
//子类的拷贝、赋值构造函数切记不忘父类成员
/**
有些成员变量的数据类型比较特别,它们的初始化方式也和普通数据类型的成员变量有所不同。
这些特殊类型的成员变量包括:
a. 引用
b. 常量
c. 静态
d. 静态常量(整型)
e. 静态常量(非整型)
常量和引用,必须通过参数列表进行初始化。
静态成员变量的初始化也颇有点特别,是在类外初始化
**/
#include <Windows.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <strsafe.h>
#include <vector>
#include <stdio.h>
#include <time.h>
#include <string>
#include"atlstr.h"
#include "3th\cyclecache\include\CycleCache.h"
#pragma comment(lib, "3th/cyclecache/bin_x64/CycleCache.lib")
#ifndef _MSC_VER
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif
using namespace std;
//缓存长度
const int BufferLength = 1024 * 1024 * 256;
std::ofstream outT("GetStatus1233.log");
INT64 g_SampleCount = 0;
int CountCRCValue(char* p, int length)//计算校验值
{
int res = 0;
for (int i = 0; i < length; i++)
{
res += *p;
p = p + 1;
}
return res;
}
template<typename T>
int count(T& x)
{
int s1 = sizeof(x);
int s2 = sizeof(x[0]);
int result = s1 / s2;
return result;
}
class testHead
{
int m_nHead;
};
class testBody
{
int m_nBody;
};
class testFoot
{
int m_nFoot;
};
class everyPerson :public testHead, public testBody, public testFoot
{
int m_nEveryPerson;
};
class testStatic
{
public:
testStatic()
{
cout << "构造测试静态类" << endl;
}
void no_static()
{
cout << "我是非静态函数" << endl;
}
static void Isstatic()
{
//no_static();
cout << "我是静态函数!!" << endl;
}
};
class BClass
{
public:
BClass() :i(1), ci(2), ri(i)
{
}
private:
int i;//普通成员变量
const int ci;//常量成员变量
int& ri;//引用成员变量
static int si;//静态成员变量
//static int si2=100;//error:只有静态常量成员变量才可以这么初始化
static const int csi;//静态常量成员变量
static const int csi2 = 100;//静态常量成员变量
static const double csd;//静态常量 非整型成员变量
static testStatic m_testStatic;
//static const double csd2=99.9;//error:只有静态常量整型成员才可以在类内初始化
static char* strofc;
};
char* BClass::strofc = "alkdsfl";
int BClass::si = 0;//静态成员变量的初始化
const int BClass::csi = 1;//静态常量成员变量的初始化
const double BClass::csd = 99.9;//静态常量成员变量的初始化。
testStatic BClass::m_testStatic;
class Base
{
public:
Base(char* _name)
{
int len = strlen(_name);
if (len == 0)
{
name = new char[1];
name[0] = '\0';
}
else
{
name = new char[len + 1];
strcpy(name, _name);
}
cout << "Base constructor " << name << endl;
}
Base(const Base& b)
{
name = new char[strlen(b.name) + 1];
strcpy(name, b.name);
cout << "Base's copy constructor" << endl;
}
Base& operator=(const Base& b)
{
if (this == &b)
return *this;
delete[]name;
name = new char[strlen(b.name) + 1];
strcpy(name, b.name);
cout << "Base assign operator=" << name << endl;
return *this;
}
virtual ~Base()
{
outT << "!!真的析构了";
cout << "~Base del" << name << endl;
outT.close();
delete[]name;
}
virtual void mf1()
{
cout << "BBBBBBBBBBB基类中无参数的mf1BBBBBBBBBB" << endl;
}
virtual void mf2()
{
cout << "BBBBBBBBBBBBB基类中无参数的mf2BBBBBBBBBBB" << endl;
}
void mf3(float aa)
{
cout << "BBBBBBBB基类中mf3(int)BBBBBBBBB" << aa << endl;
}
void mf4()
{
cout << "BBBBBBBBBBB基类中无参数的mf4BBBBBBBBBBB" << endl;
}
private:
char *name;
};
class Derived :virtual public Base
{
public:
using Base::mf1;
using Base::mf3;
using Base::mf4;
Derived(char* _name, char* _descrip) :Base(_name)
{
int len = strlen(_descrip);
if (len == 0)
{
descrip = new char[1];
descrip[0] = '\0';
}
else
{
descrip = new char[len + 1];
strcpy(descrip, _descrip);
}
cout << "Derive constructor" << endl;
}
//do remember to intitialize parent's data members in this way
Derived(const Derived& d) :Base(d)
{
descrip = new char[strlen(d.descrip) + 1];
strcpy(descrip, d.descrip);
cout << "Derive copy constructor" << endl;
}
Derived& operator=(const Derived& d)
{
if (this == &d)
return *this;
Base::operator=(d);//call this if = is overlapped in the base class
//static_cast<&Base>(*this)=d; call this if = not overlapped,and reference a must
char* temp = new char[strlen(d.descrip) + 1];
//memcpy(temp,d.descrip,strlen(d.descrip)*sizeof (char ));
strcpy(temp, d.descrip);
delete[]descrip;
descrip = temp;//将删除放在后面防止X=X特殊情况下,内容的丢失
cout << "Derived assign operator=" << descrip << endl;
return *this;
}
~Derived()
{
cout << "Derive~ del" << descrip << endl;
delete[]descrip;
}
virtual void mf1(double ddd)
{
cout << "ddddddd派生类中mf1(double ddd)dddddddd"<<ddd << endl;
}
virtual void mf2(float ffff)
{
cout << "dddddddddd派生类中mf2(float ffff)ddddddddddd" << ffff<<endl;
}
void mf3(int ttt)
{
Base::mf3(ttt);
}
void mf4()
{
cout << "ddddddddddd派生类中mf4dddddddddddd" << endl;
}
private:
char* descrip;
};
//class A {
//public:
// A(int ii):i(ii){
// cout<<"A::A()"<<endl;
// }
// ~A(){
// cout<<"A::~A()"<<endl;
// }
// void print(){
// cout<<"A::print() "<<i<<endl;
// }
// void print(int i){
// cout<<"now A.i ="<<i<<endl;
// print();
// }
// void set(int ii){
// i = ii;
// cout<<"A.set("<<i<<")"<<endl;
// }
//private:
// int i;
//};
//
//class B : public A {
//public:
// B(int a):A(15),t(a){ //父类构造函数有参数,子类构造的时候需对父类进行初始化
// cout<<"B::B()"<<endl;
// }
// ~B(){
// cout<<"B::~B()"<<endl;
// }
// void print(){
// cout<<"B::print()"<<endl;
// }
// void f(){
// cout<<"######B.f() begin###########"<<endl;
// cout<<"B.t="<<t<<endl;
// set(20);
// //i = 10; error: public继承不能直接修改基类里的private成员
// print();
// cout<<"=======B.f() over==============="<<endl;
// }
//private:
// int t;
//}; //public继承,可以访问,不能直接修改基类里的private成员.
class A
{
public:
int _theV;
A() :_theV(88)
{
}
void print()
{
cout << "A" << endl;
}
virtual void print1()
{
cout << "Virtual A" << endl;
}
void printTest()
{
cout << _theV << endl;
}
};
class B :public A{
public:
int _theV;
B()
{
_theV = 66;
}
void print() { cout << "B" << endl; }
virtual void print1() { cout << "Virtual B" << endl; }
};
void fn(A & s)
{
s.print();
s.print1();
}
//panda 测试多继承 -虚函数调用
class ZooAnimal
{
public:
ZooAnimal()
{
cout << "ZooAnimal default constructor" << endl;
}
ZooAnimal(const ZooAnimal&)
{
cout << "ZoomAniaml copy constructor " << endl;
}
virtual ~ZooAnimal()
{
cout << "Goodbye ZoomAniaml " << endl;
}
ZooAnimal& operator=(const ZooAnimal&)
{
cout << "I am ZooAnimal copy operator=" << endl;
return *this;
}
virtual void print()
{
cout << "I am ZooAnimal" << endl;
}
};
class Widget
{
public:
Widget(int widgetSize)
{
cout << "====构造widget====" << endl;
}
~Widget()
{
cout << "++++析构widget+++++" << endl;
}
void showInfo()
{
cout << "调用showInfo()函数" << endl;
}
};
Widget * constructWidgetInBuffer(void* buffer, int wisize)
{
return new (buffer)Widget(wisize);
}
class Bear : public ZooAnimal
{
public:
Bear()
{
cout << "I am Bear default constructor" << endl;
}
Bear(const Bear&)
{
cout << "I am Bear copy constructor" << endl;
}
virtual ~Bear()
{
cout << "Goodbye Bear" << endl;
}
Bear& operator=(const Bear&)
{
cout << "I am Bear copy operator=" << endl;
return *this;
}
virtual void print()
{
cout << "I am Bear" << endl;
}
virtual void toes()
{
cout << "I am Bear toes" << endl;
}
};
class Endangered
{
public:
Endangered()
{
cout << "I am Endangered default constructor" << endl;
}
Endangered(const Endangered&)
{
cout << "I am Endangered copy constructor" << endl;
}
virtual ~Endangered()
{
cout << "Goodbye Endangered " << endl;
}
Endangered& operator=(const Endangered&)
{
cout << "I am Endangered copy operator=" << endl;
return *this;
}
virtual void print()
{
cout << "I am Endangered" << endl;
}
virtual void highlight()
{
cout << "I am Endangered.highlight" << endl;
}
};
class Panda : public Bear, public Endangered
{
public:
Panda()
{
cout << "I am Panda default constructor" << endl;
}
Panda(const Panda&)
{
cout << "I am Panda copy constructor" << endl;
}
Panda& operator=(const Panda&)
{
cout << "I am Panda copy operator=" << endl;
return *this;
}
void print()
{
cout << "I am Panda print---" << endl;
}
virtual void highlight()
{
cout << "I am Panda.highlight" << endl;
}
virtual void toes()
{
cout << "I am Panda.toes" << endl;
}
virtual void cuddle()
{
cout << "I am Panda.cuddle" << endl;
}
virtual ~Panda()
{
cout << "Goodby Panda" << endl;
}
};
class father
{
public:
int publ_i;
father(int a = 1, int b = 2, int c = 3) :publ_i(a), priv_i(b), prot_i(c)
{
cout << "father constructor" << endl;
}
virtual void display()
{
cout << "[father]publ_i is " << publ_i << endl;
cout << "[father]priv_i is " << priv_i << endl;
cout << "[father]prot_i is " << prot_i << endl;
}
private:
int priv_i;
protected:
int prot_i;
};
class son : public father
{
public:
son(int a = 2) :mypriv_i(a), father(100)
{
cout << "son constructor" << endl;
}
void display()
{
cout << "[son]publ_i is " << publ_i << endl;
cout << "[son]prot_i is " << prot_i << endl;
cout << "[son]mypriv_i is " << mypriv_i << endl;
}
private:
int mypriv_i;
};
void FunTest()
{
int tmp = 0;
int ret = 5 / tmp; //0不可以做除数,无意义
}
double division(int a, int b)
{
if (b == 0)
{
throw "Division by zero condition!";
}
return (a / b);
}
class MyException:public std::exception
{
public:
MyException() = default;
MyException(const char *message)
: message_(message)
{
cout << "MyException ..." << endl;
}
MyException(const MyException &other) : message_(other.message_)
{
cout << "Copy MyException ..." << endl;
}
virtual ~MyException()
{
cout << "~MyException ..." << endl;
}
//const char *what() const
//{
// return message_.c_str();
//}
const char *what() const NOEXCEPT override
{
return "这是MyException的what()函数\n";
}
private:
string message_;
};
class MyExceptionD : public MyException
{
public:
MyExceptionD(const char *message)
: MyException(message)
{
cout << "MyExceptionD ..." << endl;
}
MyExceptionD(const MyExceptionD &other)
: MyException(other)
{
cout << "Copy MyExceptionD ..." << endl;
}
~MyExceptionD()
{
cout << "~MyExceptionD ..." << endl;
}
};
void fun(int n) throw (int, MyException, MyExceptionD)
{
if (n == 1)
{
throw 1;
}
else if (n == 2)
{
throw MyException("test Exception");
}
else if (n == 3)
{
throw MyExceptionD("test ExceptionD");
}
}
void fun2() throw()
{
}
//重写了GetLastError()消息
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40)*sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
int testRefence(int& a, int& b)
{
a--;
b--;
int c = a + b;
return c;
}
struct SS
{
int i;
int *p;
};
void PrintInt(const int&nData)
{
cout << nData << endl;
}
__int64 DiffFileTime(FILETIME time1, FILETIME time2)
{
__int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime;
__int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime;
return b - a;
}
CRITICAL_SECTION slock;
double scpuuse = 0;
void SetCpuUsage(double cpu)
{
EnterCriticalSection(&slock);
scpuuse = cpu;
LeaveCriticalSection(&slock);
}
double GetCpuUsage()
{
double cpu = 0;
EnterCriticalSection(&slock);
cpu = scpuuse;
LeaveCriticalSection(&slock);
return cpu;
}
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
int idle, kernel, user;
FILETIME idleTime1, idleTime2;
FILETIME kernelTime1, kernelTime2;
FILETIME userTime1, userTime2;
do
{
GetSystemTimes(&idleTime1, &kernelTime1, &userTime1);
Sleep(1000);
GetSystemTimes(&idleTime2, &kernelTime2, &userTime2);
idle = (int)DiffFileTime(idleTime1, idleTime2);
kernel = (int)DiffFileTime(kernelTime1, kernelTime2);
user = (int)DiffFileTime(userTime1, userTime2);
if (kernel + user == 0)
SetCpuUsage(0.0);
else
SetCpuUsage(abs((kernel + user - idle) * 100 / (kernel + user)));//(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率
} while (1);
}
void error_msg(initializer_list<string> il)
{
for (auto& beg : il)
{
string testb = beg + "11";
cout << testb.c_str();
}
}
void showDestruct()
{
Base tt("扯淡");
}
int testConvertAarray(int index, char devType[20])
{
memset(devType, 0, 20);
devType[0] = 'h';
devType[1] = 'e';
devType[2] = 'l';
devType[3] = 'l';
devType[4] = 'o';
return 1;
}
int testConvertPtr(int index, void* tmpPtr)
{
cout << (const char*)tmpPtr << endl;
return 1;
}
struct CardParam {
int m_clkTrigger;
float m_ddc;
char m_store;
};
int psome_arr[] = { 1,2,3,4 };
int v043_fun(int* p)
{
int res = *p;
cout << "内容为" <<res<< endl;
return 1;
}
CardParam valueCard[] = { {1,2.0,'a'},{3,25.0,'v'},{4,26.0,'x'} };
int v044_fun(CardParam* pt)
{
CardParam* res = pt;
cout << "内容1为" << res->m_clkTrigger << endl;
cout << "内容2为" << res->m_ddc << endl;
cout << "内容3为" << res->m_store << endl;
return 0;
}
typedef struct OPTICALLY_RESPOND_R {
UINT16 SerialNumber;//序列号
UINT8 SourceAddress;//信源地址
UINT8 DestAddress;//信宿地址
UINT16 Reserve;//保留字段
UINT8 DataType;//数据类别
UINT8 NoteLength;//正文长度
byte DevStatus;//设备状态
byte opticalStatus_12G;//12G光状态
byte opticalStatus_19_9G;//19.9G光状态
byte opticalStatus_25_9G;//25.9G光状态
byte opticalStatus_31_3G;//31.3G光状态
int opticalPower;//光功率
OPTICALLY_RESPOND_R()
{
SerialNumber = 0x0;
SourceAddress = 0x01;
DestAddress = 0x06;
Reserve = 0x00;
DataType = 0x01;
NoteLength = 0x09;
DevStatus = 0x00;//设备状态
opticalStatus_12G = 0x00;//12G光状态
opticalStatus_19_9G = 0x00;//19.9G光状态
opticalStatus_25_9G = 0x00;//25.9G光状态
opticalStatus_31_3G = 0x00;//31.3G光状态
opticalPower = 0x00;//光功率
}
} OPTICALLY_RESPOND_DATA;
void testxG()
{
Widget someWidget(2);
Widget * testWidgptr = constructWidgetInBuffer(&someWidget, 1);
}
int main()
{
testxG();
/*if (testWidgptr != nullptr)
{
delete testWidgptr;
testWidgptr = nullptr;
}*/
string s1("Nancy");
string s2("Clancy");
string& rs = s1; // rs 引用 s1
string *ps = &s1; // ps 指向 s1
rs = s2; // rs 仍旧引用s1,
// 但是 s1的值现在是
// "Clancy"
ps = &s2; // ps 现在指向 s2;
// s1 没有改变
char cons[12] = { 0x12, 0xaa, 0xab, 0x23, 0x12, 0xaa, 0xee, 0x18, 0x91, 0xbc, 0x01, 0x87 };
char consss[] = { 0xaa, 0xab, 0x23, 0x91 };
char res = CountCRCValue(cons, sizeof(cons));
printf("=======校验和=%02x==========\n", res);
OPTICALLY_RESPOND_DATA tv;
CString SourceIDstr;
CString DestIDstr;
CString DataTypestr;
CString InputPowerstr;
SourceIDstr.Format(_T("%x"), tv.SourceAddress);
DestIDstr.Format(_T("%x"), tv.DestAddress);
DataTypestr.Format(_T("%x"), tv.DataType);
InputPowerstr.Format(_T("%d"), tv.opticalPower);
printf("SourceIDstr=%s\n", SourceIDstr);
printf("DestIDstr=%s\n", DestIDstr);
printf("DataTypestr=%s\n", DataTypestr);
printf("InputPowerstr=%s\n", InputPowerstr);
cout <<"111 ----- "<< valueCard << endl;
cout <<"222 ----- "<< &valueCard << endl;
v044_fun(valueCard);
outT << "开始了";
if (!outT)
{
cout << "创建文件失败" << endl;
}
else
{
outT << "文件能打开";
}
char testinputArr[20] = { "!what" };
testConvertPtr(0, testinputArr);
//testConvertAarray(0, testinputArr);
cout << testinputArr << endl;
/*int sum1=7,sum2=8;
int res=testRefence(sum1,sum2);
cout<<"sum1="<<sum1<<"sum2="<<sum2<<endl;
cout<<"res="<<res<<endl;*/
// B bb;
Derived d1("宋江","及时雨");
d1.mf3(47);
//Derived d2(d1);
//cout<<"----------"<<endl;;
//Derived d3("李逵","黑旋风");
//d2=d3;
//cout<<"***********"<<endl;
BClass b_testStatic_value;
//B b(1000);
//b.f();
//b.print();
//b.set(10);
//cout<<"test beginning!"<<endl;
//b.print();
//b.f();
//b.A::print(200); //C++在父类和子类同时定义了一个函数名和参数表都一样的时候,只有子类一个函数,父类的同名函数全部被隐藏
//cout<<"test over!"<<endl;
//A a;
//B b;
//b.printTest();
//fn(a);
//fn(b);
//Bear *pb = new Panda();
//pb->print(); //ok: Panda::print
//// pb->cuddle(); //error: not part of Bear interface
//// pb->highlight(); //error: not part of Bear interface
//delete pb; //Panda::~Panda
//Endangered *pe = new Panda();
//pe->print(); //ok: Panda::print
//// pe->toes(); //error: not part of Endangered interface
//// pe->cuddle(); //error: not part of Endangered interface
//pe->highlight(); //ok: Panda::highlight
//delete pe; //Panda::~Panda
//son ss1;
//ss1.display();
//FunTest();
//int ret = GetLastError();
//FILE *fp = fopen("1.txt","rb");
//if(!fp)
//{
// ErrorExit(TEXT("fopen"));
//}
//cout<<errno<<endl;//errno 是记录系统的最后一次错误代码。代码是一个int型的值,在errno.h中定义
//errno底层是一个宏,表示错误码,此时输出结果为2
//注意:只有当一个库函数失败时,errno才会被设置。
//当函数成功运行时,errno的值不会被修改。
//这意味着我们不能通过测试errno的值来判断是否有错误存在。
//反之,只有当被调用的函数提示有错误发生时检查errno的值才有意义。
//int ret = GetLastError();//该函数返回调用线程最近的错误代码值,错误代码以单线程为基础来维护的,多线程不重写各自的错误代码值。
//GetLastError返回的值通过在api函数中调用SetLastError或SetLastErrorEx设置。
//函数并无必要设置上一次错误信息,所以即使一次GetLastError调用返回的是零值,也不能担保函数已成功执行。
//只有在函数调用返回一个错误结果时,这个函数指出的错误结果才是有效的。
//通常,只有在函数返回一个错误结果,而且已知函数会设置GetLastError变量的前提下,才应访问GetLastError;这时能保证获得有效的结果。
//SetLastError函数主要在对api函数进行模拟的dll函数中使用,所以对vb应用程序来说是没有意义的
//cout<<ret<<endl; //输出结果为2
//或者使用$err,hr在监视里查看错误码和错误消息(系统里找不到指定的文件 2)
//int x = 50;
//int y = 0;
//double z = 0;
//try {
// z = division(x, y);
// cout << z << endl;
//}catch (const char* msg)
//{
// cerr << msg << endl;
//}
//try
//{
// fun(2);
//}
//catch (int n)
//{
// cout << "catch int ..." << endl;
// cout << "n=" << n << endl;
//}
//catch (MyExceptionD &e)
//{
// cout << "catch MyExceptionD ..." << endl;
// cout << e.what() << endl;
//}
//catch (MyException &e)
//{
// cout << "catch MyException ..." << endl;
// cout << e.what() << endl;
//}
//char* str1="wangjun";//常量存储区
//int addr1=(int)str1;//第一个字符的地址'w'的地址
//int addr2=addr1+2;//第三个字符的地址,即'n'的地址
//char *str2=(char*)addr2;
//const char *str3=(char*)addr2;
//cout<<str2<<endl;
//SS s;
//int *p=&s.i;
//cout<<"查看p="<<p<<endl;
//p[0]=4;
//p[1]=3;
////cout<<"==再次查看s.p[1]="<<s.p[1]<<endl;
////cout<<"---查看s.p="<<s.p<<endl;
//s.p=p;
//cout<<"+++再次查看s.p="<<s.p<<endl;
//cout<<"!!!查看&s="<<&s<<endl;
//cout<<"###查看&s.i="<<&s.i<<endl;
////cout<<"***查看s.p[0]="<<s.p[0]<<endl;
////cout<<"***查看&s.p[0]="<<&s.p[0]<<endl;
////cout<<"***查看&s.p[1]="<<&s.p[1]<<endl;
////cout<<"***查看 s.p[1]="<<s.p[1]<<endl;
//cout<<"***查看typeof s.p[1]="<<typeid(s.p[1]).name();
////s.p[1]=1;
////cout<<"==再次查看s.p[1]="<<s.p[1]<<endl;
////float tt=9.2;
////float uu=9.6;
////int hh=(int)tt;
////int kk=(int)uu;
////cout<<hh<<endl<<kk<<endl;
//int i=0;
//vector<int> vec;
//for(i=0; i<10; i++)
//{
// vec.push_back(i);//10个元素依次进入,结果为10
//}
//for(unsigned int i=0; i<vec.size(); i++)
//{
// cout<<"初始化遍历:"<<vec[i]<<" " ;
//
//}
//cout<<endl;
////结果为:0,1,2,3,4,5,6,7,8,9
//vector<int>::iterator it;
//for(it = vec.begin(); it!=vec.end(); it++)
//{
// cout<<"迭代遍历:"<<*it<<" ";
//}
//cout<<endl;
////结果为:0,1,2,3,4,5,6,7,8,9
//vec.insert(vec.begin()+4,0);
// //结果为:11
// for(unsigned int i=0; i<vec.size(); i++)
// {
// cout<<"插入遍历:"<<vec[i]<<" ";
// }
// cout<<endl;
// //结果为:0,1,2,3,0,4,5,6,7,8,9
// vec.erase(vec.begin()+2);
// for(unsigned int i=0; i<vec.size(); i++)
// {
// cout<<"擦除遍历:"<<vec[i]<<" ";
// }
// cout<<endl;
// //结果为:0,1,3,0,4,5,6,7,8,9
// vec.erase(vec.begin()+3,vec.begin()+5);
//
// for(vector<int>::iterator it = vec.begin(); it!=vec.end(); it++)
// {
// cout<<"迭代遍历:"<<*it<<" ";
// }
// cout<<endl;
// //构造函数,复制构造函数(元素类型要一致),
// vector<int> A; //创建一个空的的容器
//
//
//
//
//
//
//
//
//
//vector<int> B(10,100); //创建一个10个元素,每个元素值为100
// vector<int> C(B.begin(),B.end()); //使用迭代器,可以取部分元素创建一个新的容器
// vector<int> D(C); //复制构造函数,创建一个完全一样的容器
//
// //重载=
// vector<int> E;
// E = B;
//
// //vector::begin(),返回的是迭代器
//
// vector<int> F(10); //创建一个有10个元素的容器
// for (int i = 0; i < 10; i++)
// {
// F[i] = i;
// }
//
/*
vector<int> F; //创建一个空容器
for (int i = 0; i < 10; i++)
{
F.push_back(i);
}
*/
// vector<int>::iterator BeginIter = F.begin();
// cout << *BeginIter << endl; //输出0
//
// //vector::end() 返回迭代器
// vector<int>::iterator EndIter = F.end();
// EndIter--; //向前移一个位置
// cout << *EndIter << endl; //输出9
//
// //vector::rbegin() 返回倒序的第一个元素,相当于最后一个元素
// vector<int>::reverse_iterator ReverBeIter = F.rbegin();
// cout << *ReverBeIter << endl; //输出9
//
// //vector::rend() 反序的最后一个元素下一个位置,也相当于正序的第一个元素前一个位置
// vector<int>::reverse_iterator ReverEnIter = F.rend();
// ReverEnIter--;
// cout << *ReverEnIter << endl; //输出0
//
// //vector::size() 返回元素的个数
// cout << F.size() << endl; //输出10
//
// //vector::max_size()
// cout << F.max_size() << endl; //输出1073741823,这个是极限元素个数
//
// //vector::resize()
// cout << F.size() << endl; //输出10
// F.resize(5);
// for(int k = 0; k < F.size(); k++)
// cout << F[k] << " "; //输出 0 1 2 3 4
// cout << endl;
// F.resize(16);
// F.reserve(16);
//
//
// //vector::capacity()
// cout << F.size() << endl; //5
// cout << F.capacity() << endl; //10
// for(int p=0;p< F.size();p++)
// {
// F[p]=p;
// }
// //vector::empty()
// B.resize(0);
// cout << B.size() << endl; //0
// cout << B.capacity() << endl; //10
// cout << B.empty() << endl; //true
//
// //vector::reserve() //重新分配存储空间大小
// cout << C.capacity() << endl; //10
// C.reserve(4);
// cout << C.capacity() << endl; //10
// C.reserve(14);
// cout << C.capacity() << endl; //14
//
// //vector::operator []
// cout << F[0] << endl; //第一个元素是0
//
// //vector::at()
// try
// {
// cout << "F.size = " << F.size() << endl; //5
// cout << F.at(6) << endl; //抛出异常
// }
// catch(out_of_range)
// {
// cout << "at()访问越界" << endl;
// }
//
// //vector::front() 返回第一个元素的值
// cout << F.front() << endl; //0
//
// //vector::back()
// cout << F.back() << endl; //4
//
// //vector::assign()
// cout << A.size() << endl; //0
// vector<int>::iterator First = C.begin();
// vector<int>::iterator End = C.end()-2;
// A.assign(First,End);
// cout << A.size() << endl; //8
// cout << A.capacity() << endl; //8
//
// A.assign(5,3); //将丢弃原来的所有元素然后重新赋值
// cout << A.size() << endl; //5
// cout << A.capacity() << endl; //8
//
// //vector::push_back()
// cout << *(F.end()-1) << endl; //4
// F.push_back(100);
// cout << *(F.end()-1) << endl; //100
//
// //vector::pop_back()
// cout << *(F.end()-1) << endl; //100
// F.pop_back();
// cout << *(F.end()-1) << endl; //4
//
// //vector::swap()
// F.swap(D); //交换这两个容器的内容
// for(int f = 0; f < F.size(); f++)
// cout << F[f] << " ";
// cout << endl;
// for (int d = 0; d < D.size(); d++)
// cout << D[d] << " ";
// cout << endl;
// //vector::clear()
// F.clear();
// cout << F.size() << endl; //0
// cout << F.capacity() << endl; //10
//
// unsigned short* tstAarry=new unsigned short[100];
//
// cout<<"查看数组大小"<<count(tstAarry)<<endl;
//
// delete[] tstAarry;
//
//
// vector<int> testvec;
// testvec.push_back(1);
// testvec.push_back(2);
// cout<<"查看testvec大小"<<testvec.size()<<endl;
////cout<<"查看testvec大小"<<testvec.at(3)<<endl;
//
// for(int mm=0;mm<4;mm++)
// {
// for(int cc=0;cc<8;cc++)
// {
// cout<<mm<<"****"<<cc;
// cout<<endl;
// }
// cout<<endl;
// }
vector<int> vecInt;
for (int i = 0; i < 10; ++i)
{
vecInt.push_back(i);
}
cout << "向量中的内容为:" << endl;
//for_each(vecInt.begin(),vecInt.end(),PrintInt);
for (vector<int>::iterator iter = vecInt.begin(); iter != vecInt.end(); ++iter) {
cout << *iter << " ";
}
cout << "\n";
cout << "vector contains " << vecInt.size() << " elements" << endl;
vecInt.pop_back();//删除最后一个元素
cout << "删除最后一个元素后,vector contains " << vecInt.size() << " elements" << endl;
vector<int>::iterator k = vecInt.begin();
vecInt.erase(k);//删除第一个元素
for (vector<int>::iterator iter = vecInt.begin(); iter != vecInt.end(); ++iter) {
cout << *iter << " ";
}
cout << "\n";
//vecInt.erase(k); //迭代器k已经失效,会出错
cout << "删除第一个元素后,vector contains " << vecInt.size() << " elements" << endl;
k = vecInt.begin();
vecInt.erase(k);
for (vector<int>::iterator iter = vecInt.begin(); iter != vecInt.end(); ++iter) {
cout << *iter << " ";
}
cout << "\n";
cout << "删除第一个元素后,vector contains " << vecInt.size() << " elements" << endl;
vecInt.insert(vecInt.begin() + 3, 2, 99);
cout << "插入元素后,vector contains " << vecInt.size() << " elements" << endl;
for (vector<int>::iterator iter = vecInt.begin(); iter != vecInt.end(); ++iter) {
cout << *iter << " ";
}
//vecInt.erase(vecInt.begin(),vecInt.end()); //删除所有元素
//cout<<"删除所有元素后,vector contains "<<vecInt.size()<<"elements"<<endl; //输出为0
//cout<<"删除元素后,vector contains "<<vecInt.size()<<" elements"<<endl;
//cout<<"向量开始到新结束为止的元素:"<<endl;
//for_each(vecInt.begin(),vecNewEnd,PrintInt);
//cout<<"向量中的元素:"<<endl;
//for_each(vecInt.begin(),vecInt.end(),PrintInt);
//HANDLE m_hThread;
//InitializeCriticalSection(&slock);
//m_hThread = CreateThread(NULL, 0, ThreadProc, 0, 0, NULL);//创建新线程
//do
//{
// printf("cpu useage: %01f!\n", GetCpuUsage());
// Sleep(1000);
//} while (1);
//CloseHandle(m_hThread);
//DeleteCriticalSection(&slock);
char *revBufStr = "动态链接库(dll)概述没接触dll之前觉得它很神秘,就像是一个黑盒子,既不能直接运行,也不能接收消息。它们是一些独立的文件,其中包含能被可执行程序或其他dll调用来完成某项工作的函数,";
/*char *revBufStr = "动态链接库(dll)概述\
没接触dll之前觉得它很神秘,就像是一个黑盒子,既不能直接运行,也不能接收消息。\
它们是一些独立的文件,其中包含能被可执行程序或其他dll调用来完成某项工作的函数,\
只有在其他模块调用dll中的函数时,dll才发挥作用。在实际编程中,\
我们可以把完成某项功能的函数放在一个动态链接库里,然后提供给其他程序调用。\
像Windows API中所有的函数都包含在dll中,如Kernel32.dll, User32.dll, GDI32.dll等。\
那么dll究竟有什么好处呢? 静态库和动态库静态库:函数和数据被编译进一个二进制文件(扩展名通常为.lib)\
, 在使用静态库的情况下,在编译链接可执行文件时,链接器从静态库中复制这些函数和数据,\
并把它们和应用程序的其他模块组合起来创建最终的可执行文件(.exe)。\
当发布产品时,只需要发布这个可执行文件,并不需要发布被使用的静态库。\
动态库:在使用动态库时,往往提供两个文件:一个引入库(.lib,非必须)和一个.dll文件。\
这里的引入库和静态库文件虽然扩展名都是.lib,但是有着本质上的区别,对于一个动态链接库来说,\
其引入库文件包含该动态库导出的函数和变量的符号名,而.dll文件包含该动态库实际的函数和数据。\
使用动态链接库的好处可以使用多种编程语言编写:比如我们可以用VC++编写dll,然后在VB编写的程序中调用它\
。增强产品功能:可以通过开发新的dll取代产品原有的dll,达到增强产品性能的目的。\
比如我们看到很多产品踢动了界面插件功能,允许用户动态地更换程序的界面,这就可以通过更换界面dll来实现。\
提供二次开发的平台:用户可以单独利用dll调用其中实现的功能,来完成其他应用,实现二次开发。\
节省内存:如果多个应用程序使用同一个dll,该dll的页面只需要存入内存一次,\
所有的应用程序都可以共享它的页面,从而节省内存。\dll的创建dll的创建主要有两种方法:\
一是使用 __declspec(dllexport) 创建dll,二是使用模块定义(.def)文件创建dll。\
使用 __declspec(dllexport) 创建dll首先在VS中的Visual C++中创建一个Win32 Project,\
取名为Dll1。在Application Type中选择DLL,在Additional options中选择Empty project,\
\即创建一个空的动态链接库工程。";*/
cout << "------------------------------------------" << endl;
MyException mme;
std::exception *pp = &mme;
try
{
throw mme;
}
catch (std::exception& ex)
{
std::cout << ex.what() << endl;
}
try
{
throw *pp;
}
catch (std::exception& ex)
{
std::cout << ex.what() << endl;
}
int ia[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //ia是一个含有十个整数的数组
auto beginptr=begin(ia);
auto endptr = end(ia);
cout << "C++11 测试标准库begin函数-->" << *beginptr << endl;
cout << "C++11 测试标准库end函数-->" << *(endptr-1) << endl;
//int kkk = { 2.15 };
//如果左侧对象是内置类型,那么初始值列表最多只能包含一个值,
//而且该值即使转换的话其所占空间也不应该大于目标类型的空间。
int * pintTe = NULL;
double* pdoubleTe = NULL;
MyException* pmmex=nullptr;
cout << "查看pintTe:" << sizeof(pintTe) << endl;
cout << "查看*pintTe:" << sizeof(*pintTe) << endl;
cout << "查看pdoubleTe:" << sizeof(pdoubleTe) << endl;
cout << "查看*pdoubleTe:" << sizeof(*pdoubleTe) << endl;
cout << "变量还没有创建,查看sizeof pmmex:" << sizeof(pmmex) << endl;
cout << "变量还没有创建,查看sizeof *pmmex:" << sizeof(*pmmex) << endl;
const size_t sz = sizeof(ia) / sizeof(*ia);
int arr2[sz];
vector<int> vv = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//范围变量必须是引用类型,这样才能对元素执行写操作
for (auto &r : vv)
{
r *= 2;
cout << r << endl;
}
const int testNc = 5;
int testcc = testNc;
testcc += 67;
initializer_list<string> ls;
initializer_list<int> li;
error_msg({ "functionX", "is", "over" });
error_msg({ "functionX", "is", "okay" });
cout << "-----------0-------------" << endl;
everyPerson* pevPerson = new everyPerson;
testHead *ptestHead = dynamic_cast<testHead *>(pevPerson);
testBody *ptestBody = dynamic_cast<testBody *>(pevPerson);
testFoot *ptestFoot = dynamic_cast<testFoot *>(pevPerson);
cout << "ptestHead" << ptestHead << endl;
cout << "ptestBody" << ptestBody << endl;
cout << "ptestFoot" << ptestFoot << endl;
cout << "pevPerson" << pevPerson << endl;
cout << "-----------1-------------"<< endl;
cout << "(int)ptestHead" << (int)ptestHead << endl;
cout << "(int)ptestBody" << (int)ptestBody << endl;
cout << "(int)ptestFoot" << (int)ptestFoot << endl;
cout << "(int)pevPerson" << (int)pevPerson << endl;
cout << "-----------2-------------" << endl;
if (ptestHead == pevPerson)
{
cout << "person隐式转换到头" << endl;
}
if (ptestBody == pevPerson)
{
cout << "person隐式转换到身体" << endl;
}
if (ptestFoot == pevPerson)
{
cout << "person隐式转换到脚" << endl;
}
A jjjjj;
jjjjj.printTest();
//showDestruct();
Base tt("扯淡111");
// system("pause");
Sleep(6000);
//getchar();
return 0;
}