#include <cstdio>
#include <iostream>
#include <Windows.h>
//#define _AFXDLL
//#include "afx.h"
#include <strsafe.h>
#include <memory>
#include <functional>
#include <algorithm>
#include <map>
#include <utility>
#include <experimental/vector>
#include "myclass.h"
#include "test.h"
using namespace std;
struct A
{
short a;
int b;
char c;
};
struct B
{
char a;
A b;
short c;
virtual void vFun()
{
cout << "B" << endl;
}
};
struct C : public B
{
char d;
short e;
int f;
virtual void vFun()
{
cout << "C" << endl;
}
void foo()
{
cout << "foo c = " << c << " typename = " << typeid(this).name() << endl;
}
};
void main()
{
cout << "sizeof A = " << sizeof(A) << " sizeof B = " << sizeof(B) << endl;
//方法一输出调试信息
//OutputDebugString("sizeof A = %d, sizeof B = %d\n");
char sz[1024];
StringCchPrintf(sz, _countof(sz), TEXT("%s%d%s%d\n"), "FUN1:sizeof A = ", sizeof(A), " sizeof B = ", sizeof(B));
OutputDebugString(sz);
//方法二输出调试信息
//TRACE2("FUN2:sizeof A = %d sizeof B = %d\n", sizeof(A), sizeof(B));
B * ptr_BFromC = dynamic_cast<B *>(new C);
A * ptr_A = new A;
C * ptr_C = dynamic_cast<C *>(ptr_BFromC);
B * ptr_B = new B;
C * ptr_CFromStaB = static_cast<C *>(ptr_B);
C * ptr_CFromB = dynamic_cast<C *>(ptr_B);
cout << "ptr_CFromStaB = " << ptr_CFromStaB << " ,ptr_CFromB = " << ptr_CFromB << endl;
shared_ptr<A> sPtr_A(ptr_A);
weak_ptr<A> wPtr_A(sPtr_A);
shared_ptr<A> temp = wPtr_A.lock();
cout << "user_num = " << sPtr_A.use_count() << " a = " << temp->a << endl;
cout << "typeid = " << typeid(ptr_BFromC).name() << endl;
ptr_BFromC->vFun();
ptr_C->vFun();
ptr_B->vFun();
delete ptr_B;
cout << "c = " << ptr_CFromStaB->c << endl;
//ptr_CFromStaB->vFun();
ptr_CFromStaB->foo();
unsigned uInt = -1;
unsigned long long uLLInt = -1;
cout << "type = " << typeid(uInt).name() << "uInt value = " << uInt << " uLL value = " << uLLInt << endl;
int _array[] = { 1, 2, 3 };
for (auto& i : _array)
{
i *= 2;
cout << "i = " << i << endl;
}
for each (auto a in _array)
{
cout << "a = " << a << endl;
}
map<int, int> iMap;
iMap.insert(std::make_pair<int, int>(1, 1));
iMap.insert(make_pair(2, 0)); //std::experimental::erase_if(iMap, [](int i) {return i == 1; });
for (auto i : iMap)
{
cout << i.first << endl;
}
//std::function
auto res = [](int i, int def = 10) {
cout << "lamada test:i = " << i + def << endl; return 0;
};
res(100);
res(100, 100);
for_each(_array, _array + 3, res);
auto res1 = [](int& i) {
cout << "lamada test1:i = " << ++i << endl;
};
for_each(_array, _array + 3, res1);
auto res2 = [](int i) {
cout << "lamada test2:i = " << i << endl;
};
for_each(_array, _array + 3, res2);
{
myClass mi(100);
mi.getId();
mi.getId();
mi.func();
}
testPrint();
cout << "------------------------------------" << endl;
int aa = 1;
int & bb = aa;
int * pa = &aa;
int * pb = &bb;
cout << "pa = " << pa << " pb = " << pb << endl;
//最简单的lambda表达式调用
[] {}();
vector<int> myVec;
myVec.push_back(1);
myVec.push_back(2);
myVec.push_back(3);
myVec.push_back(4);
myVec.push_back(5);
myVec.push_back(6);
std::remove_if(myVec.begin(), myVec.end(), [](int i) {return !(i % 2); });
//remove只是将vector中满足条件的元素位置移动,改变了逻辑大小,并没有改变vector的物理大小,只有erase才会改变其物理大小,
//参考https://zh.cppreference.com/w/cpp/algorithm/remove
//myVec.erase(std::remove_if(myVec.begin(), myVec.end(), [](int i) {return !(i % 2); }), myVec.end());
cout << "after remove: sizeof(myVec) = " << myVec.size() << endl;
for (auto i : myVec)
{
cout << i << endl;
}
std::experimental::erase_if(myVec, [](int i) {return !(i%2);});
cout << "after erase: sizeof(myVec) = " << myVec.size() << endl;
for (auto i : myVec)
{
cout << i << endl;
}
system("pause");
return;
}
*********************************************************************
#include "myclass.h"
#include "test.h"
void myClass::func()
{
myPrint();
}
void myClass::myPrint()
{
testPrint();
cout << "myClass myPrint" << endl;
}
*********************************************************************
#ifndef MYCLASS_H
#define MYCLASS_H
#include <iostream>
using namespace std;
class myClass
{
public:
int id;
myClass(int _id)
:id(_id), count(0)
{
cout << "construction" << endl;
}
virtual ~myClass()
{
cout << "destruction" << endl;
}
int getId() const
{
count++;//mutable突破const修饰函数的限制,mutable修饰的count私有成员变量才可以修改
cout << "count = " << count << endl;
return id;
}
void func();
void myPrint();
private:
mutable int count;//mutable突破const限制
};
#endif
*****************************************************************
#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <cstdio>
void testPrint();
#endif
**********************************************************
#include "test.h"
using namespace std;
void testPrint()
{
char str[6] = "hello";
char dest[6] = {0};
cout << "before memcpy: length of dest=" << strlen(dest) << " sizeof dest=" << sizeof(dest) << endl;
memcpy_s(dest, sizeof(dest), str, 6);
printf("%s %s\n", str, dest);
cout << "after memcpy: length of dest=" << strlen(dest) << " sizeof dest=" << sizeof(dest) << endl;
}
*****************************************************