指针
指针是一种数据类型,存放地址的手段。
空指针
大多数情况定义的是
#define NULL 0
但需要传入 NULL 的地方 不一定可以传入 0 , 它们使用的方法不是完全等价的。
在C++中对NULL重新做了定义 NULL ---> nullptr
其他指针
this指针,函数指针
函数指针: 一个指向函数的指针
int add(int a,int b)
{
return a+ b;
}
int main()
{
// int add(int ,int ); //这是一个函数的声明
int (*p)(int , int ) = nullptr; //初始化一个函数指针指向bullptr
p = add; //地址值的赋值
cout << p(10,20) << endl; //cout << add(10,20) << endl;
}
利用函数指针制作一个方便的可以输出不同情况数组的示例:
bool fn(int n )
{
return true;
}
int printfArray(int *a, int len,bool (*pfn)(int))
{
for(int i = 0 ; i < len ; ++i)
{
if(pfn(*(a+i)))
{
cout << *(a+i) << endl;
}
}
return 0;
}
这个情况可以在printfArray的第三个参数部分写入对数组判断条件的函数。极大的降低了耦合性。
强制类型转换的方法
int intcmp(const void *p1,const void *p2)
{
//int *q1 = (int *)p1;
// int *q2 = (int *)p2;
//由函数调用可知,要将const void * 转换成int *
int *q1 = reinterpret_cast<int *>const_cast<void*>(p1); //reinterpret_cast 把void* 转换成 int* const_cast<void*> 把const void* 转换成了void*
int *q2 = reinterpret_cast<int *>const_cast<void*>(p2);
return *q1-*q2;
}
lamda表达式
[](){}
[capture] (argument) {body} //它的内容可以为空
//对它的使用 秉承着短小函数 就地编写
[capture]
//闭包: lamda 里面的对象是和外面没有关系的,内层作用域无法使用外层的内容
// 而且[capture] 中捕获的值 不能进行修改 除非加上引用
// eg: [i] 不能修改i的值
// [&i] 可以修改i的值
// [&] 将前面所有定义的对象全部捕获
int i =1 ;
auto fn = [i](int n){cout << n+i << endl;}; //[i] 捕获了i的值 在lamda中可以使用i但不能修改
auto fn = [& i](int n){cout << n+i << endl;}; //将i的前面加上引用的符号 在lamda中可以修改i
以上面的输出数组为例:
printfArray(a,len,[](int n){return n % 3 == 0;});
//在没有捕获的情况下 lamda函数可以作为指针使用
这里表示的就是输出数组中能被3整除的数。
[](int n){cout << n << endl;}; //记得里面的表达式也要写上分号!
//调这个函数有两种方法
//1
[](int n){cout << n << endl;}(100); //这里就调用了这个lemda函数 并且传入n的值100
//2
auto fn = [](int n){cout << n << endl;}
fn(100); //这里就调用了这个lemda函数 并且传入n的值100
深复制与浅复制
函数传参或者调用的时候新复制的指针,只是创建了一个新的地址指向原来同样的地址,没有将里面的内容进行复制-------> 浅复制。
深复制不止创建了地址,还复制了一份同样的数据出来。
OTHER
//链表
//struct所有的成员都是共有的
struct Node
{
Node(int value = 0,Node *p = nullptr)
:data(value),next(p){}
int data;
Node * next;
};
class List
{
public:
List():pHead(nullptr){}
List(const List &other):pHead(nullptr)
{
Node *p = other.pHead;
while(p!= nullptr)
{
this->push_back(p->data);
p =p->next;
}
}
void push_front(int n)
{
Node *pNew =new Node(n);
pNew->next = pHead;
pHead = pNew;
}
size_t size() const
{
int i = 0;
Node *p = pHead;
while(p != nullptr)
{
++i;
p = p->next;
}
return i;
}
bool IsEmpty()
{
if(pHead == nullptr)
return true;
return false;
}
void show()
{
Node *p = pHead;
while(p!= nullptr)
{
cout << p->data<< endl;
p = p->next;
}
}
void push_back(int n)
{
if(IsEmpty())
{
push_front(n);
}
else{
Node *pNew =new Node(n);
Node * p = pHead;
while(p->next!= nullptr)
{
p = p->next;
}
p->next = pNew;
}
}
void pop_front(void)
{
if(!IsEmpty())
{
Node* p = pHead;
pHead = p->next;
delete p;
}
}
void pop_back()
{
if(size()>=2)
{
Node*p = pHead;
while(p->next->next!=nullptr)
{
p = p->next;
}
delete p->next;
p->next = nullptr;
}
else
{
pop_front();
}
}
void clear_all()
{
while(!IsEmpty())
{
pop_front();
}
}
~List()
{
clear_all();
cout << "out over" << endl;
}
private:
Node *pHead;
};
//字符串
class String
{
public:
String(const char *p):m_p(new char [strlen(p)+1])
{
strcpy(m_p,p);
}
String (const String &other):m_p(new char [strlen(other.m_p)+1])
{
strcpy(this->m_p,other.m_p);
}
void show()
{
cout << m_p << endl;
}
size_t length()
{
return strlen(m_p);
}
void append(const char *p)
{
char *q = new char [strlen(m_p)+ strlen(p)+1];
strcpy(q,m_p);
strcat(q,p);
delete []m_p;
m_p = q;
}
void append(const String &other)
{
char *p = new char [strlen(m_p)+strlen(other.m_p)+1];
strcpy(p,m_p);
strcat(p,other.m_p);
delete []m_p;
m_p = p;
}
void assign(const char *p)
{
char *q = new char[strlen(p) + 1];
strcpy(q,p);
delete []m_p;
m_p = q;
}
void assign(const String &other)
{
if (this != &other)
{
char *p = new char[strlen(other.m_p) + 1];
strcpy(p,other.m_p);
delete []m_p;
m_p = p;
}
}
~String()
{
delete []m_p;
}
private:
char *m_p;
};
一些简单的类的抽象