宏及pragma once解决头文件重复包含
#include <iostream>
using namespace std;
#include "AA.h"
#include "BB.h"
int main(){
CAA aa;
CBB bb;
system("pause");
return 0;
}
#pragma once
class CAA
{
public:
protected:
private:
};
#pragma once
#include "AA.h"
class CBB
{
public:
CAA m_a;
protected:
private:
};
宏、“/”、“#”、“##”的作用
#include <iostream>
using namespace std;
#include "6-2-2.h"
#include "test.h"
int main() {
AA(8)
cout << 2 << endl;
BB(2)
BB("2")
CC(CTest1)
CC(CTest2)
system("pause");
return 0;
}
#pragma once
#define N 10
#define AA(PARAM) \
for (int i = 0; i < PARAM; i++) \
{ \
cout << i << endl; \
}
#define BB(PARAM) \
cout<<#PARAM<<endl;
#define CC(THIS_CLASS)\
THIS_CLASS tst##THIS_CLASS;\
tst##THIS_CLASS.show();
#pragma once
#include <iostream>
using namespace std;
class CTest1 {
public:
void show() {
cout << "CTest1::show" << endl;
}
};
class CTest2 {
public:
void show() {
cout << "CTest2::show" << endl;
}
};
重载操作符之类内重载
#include <iostream>
using namespace std;
class CTest
{
public:
int m_a;
CTest() {
m_a = 10;
}
int operator+(int a) {
return m_a + a;
}
int operator=(int a) {
this->m_a = a;
return m_a;
}
int operator+=(int a) {
this->m_a += a;
return m_a;
}
int operator++() {
return ++this->m_a;
}
int operator++(int) {
return this->m_a++;
}
protected:
private:
};
int main() {
CTest tst;
CTest tst1;
tst + 10;
int a = tst1 + (tst + 10);
cout << a << endl;
CTest tst2;
tst2 = tst1 + (tst + 10);
cout << tst2.m_a << endl;
CTest tst3;
tst3 = tst1.operator+(12);
cout << tst3.m_a << endl;
CTest tst4;
CTest tst5;
tst5 = tst4 += 20;
cout << tst4.m_a << endl;
cout << tst5.m_a << endl;
CTest tst6;
CTest tst7;
tst7 = ++tst6;
cout << tst7.m_a << endl;
CTest tst8;
CTest tst9;
tst9 = tst8++;
cout << tst9.m_a << endl;
system("pause");
return 0;
}
重载操作符之类外重载
class CTest
{
public:
int m_a;
CTest() {
m_a = 10;
}
};
int operator+(int a, CTest& tst) {
return a + tst.m_a;
}
int operator++(CTest& tst) {
return ++tst.m_a;
}
int operator++(CTest& tst,int) {
return tst.m_a++;
}
ostream& operator<<(ostream& os, CTest& tst) {
os << tst.m_a;
return os;
}
istream& operator>>(istream& is, CTest& tst) {
is >> tst.m_a;
return is;
}
int main() {
CTest tst;
int a = 10 + tst;
cout << a << endl;
cout << ++tst.m_a << endl;
cout << tst.m_a++ << endl;
cout << tst << endl;
cin >> tst;
cout << tst << endl;
system("pause");
return 0;
}
迭代器概念引入(改造遍历链表~)
struct Node {
int val;
Node* pNext;
Node(int v) {
val = v;
pNext = NULL;
}
};
class CMyIterator {
public:
Node* pTemp;
public:
CMyIterator() {
pTemp = NULL;
}
CMyIterator(Node* pH) {
pTemp = pH;
}
public:
bool operator==(Node* pNode) {
return pTemp == pNode ? true : false;
}
bool operator!=(Node* pNode) {
return pTemp != pNode ? true : false;
}
Node* operator=(Node* pNode) {
pTemp = pNode;
return pTemp;
}
int operator*() {
if (pTemp) {
return pTemp->val;
}
return -1;
}
Node* operator++() {
pTemp = pTemp->pNext;
return pTemp;
}
Node* operator++(int) {
Node* pT = pTemp;
pTemp = pTemp->pNext;
return pT;
}
};
class MyList
{
public:
Node* m_pHead;
Node* m_pEnd;
int m_nLen;
public:
MyList() {
m_pHead = NULL;
m_pEnd = NULL;
m_nLen = 0;
}
~MyList() {
Node* pTemp = NULL;
while (m_pHead) {
pTemp = m_pHead;
m_pHead = m_pHead->pNext;
delete pTemp;
pTemp = NULL;
}
m_pHead = NULL;
m_pEnd = NULL;
m_nLen = 0;
}
public:
void PushBack(int v) {
Node* pTemp = new Node(v);
if (m_pHead) {
m_pEnd->pNext = pTemp;
m_pEnd = m_pEnd->pNext;
}
else {
m_pHead = pTemp;
m_pEnd = pTemp;
}
m_nLen++;
}
void show() {
CMyIterator ite;
ite = m_pHead;
while (ite != NULL) {
cout << *ite << " ";
++ite;
}
cout << endl;
}
};
int main() {
MyList lst;
lst.PushBack(1);
lst.PushBack(2);
lst.PushBack(3);
lst.PushBack(4);
lst.show();
system("pause");
return 0;
}
list及algorithm头文件
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
void Show(int a) {
cout << a << " ";
}
int main() {
list<int> lst;
lst.push_back(1);
lst.push_back(2);
lst.push_back(3);
lst.push_front(0);
lst.push_front(-1);
lst.push_front(-2);
list<int>::iterator ite = lst.begin();
while (ite != lst.end())
{
cout << *ite << endl;
ite++;
}
::for_each(lst.begin(), lst.end(), &Show);
cout << endl;
lst.pop_back();
::for_each(lst.begin(), lst.end(), &Show);
cout << endl;
lst.pop_front();
::for_each(lst.begin(), lst.end(), &Show);
cout << endl;
list<int>::iterator ite1 = ++lst.begin();
list<int>::iterator ite2 = lst.insert(ite1, 5);
::for_each(lst.begin(), lst.end(), &Show);
cout << endl;
cout << *ite2 << endl;
ite2 = lst.erase(ite2);
::for_each(lst.begin(), lst.end(), &Show);
cout << endl;
cout << *ite2 << endl;
cout << lst.front() << endl;
list<int>::iterator ite3 = lst.begin();
cout << *ite3 <<endl;
cout << lst.back() << endl;
list<int>::iterator ite4 = lst.end();
--ite4;
cout << *ite4 << endl;
list<int>::iterator ite5 = ::find(lst.begin(), lst.end(), 1);
cout << *ite5 << endl;
system("pause");
return 0;
}
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
struct Node
{
int m_a;
int m_b;
Node() {
m_a = 10;
m_b = 20;
}
};
void Show(Node* p) {
cout << p->m_a << " " << p->m_b << " ";
}
int main() {
list<Node*> lst1;
Node* p1 = new Node;
lst1.push_back(p1);
Node* p2 = new Node;
p2->m_a = 30;
p2->m_b = 40;
lst1.push_back(p2);
list<Node*>::iterator ite = lst1.begin();
while (ite != lst1.end())
{
cout << (*ite)->m_a << " " << (*ite)->m_b << " ";
ite++;
}
cout << endl;
::for_each(lst1.begin(), lst1.end(), &Show);
cout << endl;
system("pause");
return 0;
}