返回左值
#include<iostream>
using namespace std;
//返回变量本身
int & fun1()
{
//静态变量最好初始化
static int a1 = 0;
return a1;
}
//返回变量值
int fun2()
{
static int a2 = 1;
return a2;
}
int main()
{
int b1,b2;
b1=fun1();
cout<<"b1 = "<<b1<<endl;
fun1()=10;
//充当左值
b1 = fun1();
b2 = fun2();
cout<<"b1 = "<<b1<<endl;
cout<<"b2 = "<<b2<<endl;
char c;
cin>>c;
}
指针引用
#include<iostream>
using namespace std;
struct Teacher
{
char name[20];
int age;
};
//在被调用函数,获取资源
int getTeacher(Teacher **p)
{
Teacher *tmp = NULL;
if(p == NULL)
{
return -1;
}
tmp=(Teacher *)malloc(sizeof(Teacher));
if(tmp == NULL)
{
return -2;
}
tmp->age = 33;
*p = tmp;
return 0;
}
//指针的引用,做函数参数
int getTeacher2(Teacher* &myp)
{
myp=(Teacher *)malloc(sizeof(Teacher));
if(myp == NULL)
{
return -1;
}
myp->age=36;
return 0;
}
void FreeTeacher(Teacher * p1)
{
if(p1 == NULL)
{
return ;
}
free(p1);
}
int main()
{
Teacher *p1 = NULL;
getTeacher(&p1);
cout<<"age:"<<p1->age<<endl;
FreeTeacher(p1);
getTeacher2(p1);
cout<<"age:"<<p1->age<<endl;
FreeTeacher(p1);
char c;
cin>>c;
}
const引用
#include<iostream>
using namespace std;
int main()
{
double val = 0.5;
const int & rep = val;
double &rep2 = val;
cout<<"rep="<<rep<<" "<<"rep2="<<rep2<<endl;
val = 1.5;
cout<<"rep="<<rep<<" "<<"rep2="<<rep2<<endl;
char c;
cin>>c;
}
简单struct结构
#include<iostream>
using namespace std;
struct Time
{
int year;
int month;
int day;
};
void init( Time & T)
{
cout<<"please input year,month,day"<<endl;
cin>>T.year>>T.month>>T.day;
}
void print(Time & T)
{
cout<<"year:"<<T.year<<"-month:"<<T.month<<"- day:"<<T.day<<endl;
}
int main()
{
Time T;
init(T);
print(T);
char c;
cin>>c;
}
auto自动推断类型
auto i = true;
constexpr定义常量表达式
constexpr double GetPi(){ return 22.0/7;}
#define定义常量
#define pi 3.14159
动态数组
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int>Array[3];
Array[0]= 1;
Array[1]=2;
Array[2]=3;
cout<<"Array 's size:"<<Array.size()<<endl;
cout<<"Enter another element to insert"<<endl;
int newValue=0;
cin>>newValue;
Array.push_back(newValue);
cout<<"Array's size::"<<Array.size()<<endl;
cout<<Array[Array.size()-1]<<endl;
}
std::string
#include<iostream>
#include<string>
using namespace std;
int main()
{
string greetString{"Hello World"};
cout<<greetString<<endl;
cout<<"Enter a line of text"<<endl;
string inputLine
getline(cin, inputline);
string concatString = greetString+inputLine;
cout<<concatString<<concatString.length()<<endl;
}
for遍历数组元素
#include<iostream>
#include<string>
using namespace std;
int main()
{
int someNums[]={1,2,3,4,5};
for(const int&aNum:someNums)
cout<<aNum<<" ";
cout<<endl;
for(auto anElement : {1,2,3,4,5})
cout<<anElement<<" ";
cout<<endl;
string sayHello{"Hello World!"};
for(auto anElement:sayHello)
cout<<anElement<<" ";
cout<<enld;
return 0;
}
数据结构
合并两个数组,无重复
/*
实现两个列表合并消除重复元素
*/
#include<iostream>
#include<vector>
using namespace std;
int LocateElem(vector<int> ListA, int e) //元素e是否在ListA中,如果存在返回下标,否则返回0
{
for (int index = 0; index < ListA.size(); index++)//vector如果加[] 有data,第一次了解
{
if (e == ListA[index])
{
return index + 1;
}
}
return 0;
}
vector<int> ListUnion(vector<int>La,vector<int>Lb)
{
int La_length = La.size();
int Lb_length = Lb.size();
for (int index = 0; index < Lb_length; index++)
{
int e = Lb[index];
if (!LocateElem(La, e))
La.push_back(e);
}
return La;
}
void ShowList(vector<int> list)
{
for (int index = 0; index < list.size(); index++)
{
cout << list[index] << " , ";
}
}
int main()
{
vector<int> La;
/*
int a[]={1,2,3,4,5};
La(a,a+5);
*/
for (int index = 1; index <= 5; index++)
{
La.push_back(index);
}
int b[] = { 4,5,6,7 };
vector<int>Lb(b, b + 4);
La=ListUnion(La, Lb);
ShowList(La);
system("pause");
}
合并两个有序数组
/*
合并两个有序的数组,从小到大,有重复。
*/
#include<iostream>
#include<vector>
using namespace std;
int *MergeList(int *La,int lenLa,int *Lb,int lenLb)
{
int i = 0, j = 0, k = 0;
int * Lc=new int[lenLa+lenLb+1];
while ((i <lenLa) && (j <lenLb))
{
if (La[i] <= Lb[j])
{
Lc[k++] = La[i++];
}
else
{
Lc[k++] = Lb[j++];
}
}
while (i < lenLa)
{
Lc[k++] = La[i++];
}
while (j < lenLb)
{
Lc[k++] = Lb[j++];
}
return Lc;
}
void ShowList(int *list,int length)
{
for (int index = 0; index <length; index++)
{
cout << list[index] << " , ";
}
}
int main()
{
int a[] = { 1,2,3,4,5,6 };
int b[] = { 7,8,9 };
int lenA = sizeof(a) / sizeof(int);
int lenB = sizeof(b) / sizeof(int);
ShowList(MergeList(a,lenA,b,lenB),lenA+lenB);
system("pause");
}
线性列表 增删改查
//线性列表 元素的增删改查
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
Node *CreateList() //创建单链表
{
int value, length;
Node *Head = (Node*)malloc(sizeof(Node));
Node *Tail = Head;
Tail->next = NULL;
if (Head == NULL)
{
cout << "分配内存失败";
exit(-1);
}
cout << "请输入列表大小length=:";
cin >> length;
for (int index = 0; index < length; index++)
{
Node *New = (Node*)malloc(sizeof(Node));
if (New == NULL)
{
cout << "分配内存失败";
exit(-1);
}
cout << "请输入节点的值" << endl;
cin >> value;
New->data = value;
Tail->next = New;
New->next = NULL;
Tail = New;
}
return Head;
}
void ShowList(Node*Head)
{
Node*p = Head->next;
while (p != NULL)
{
cout << p->data << endl;
p = p->next;
}
}
//查询链表中指定位置的元素
int GetElem_List(Node* list, int index,int &e)//&引用
{
Node *p = list->next;
int j = 1;
while (p&&j < index)
{
p = p->next;
j++;
}
if (!p || j > index) return -1;
e = p->data;
return 1;
}
//改
int ChangeElem_List(Node*list, int index, int &e)//在第index位置变为元素e
{
Node*p = list;
int j = 0;
while (p->next&&j < index-1 )
{
p = p->next;
j++;
}
if (!p || j > index) return -1;
Node*New = (Node*)malloc(sizeof(Node));
New->data = e;
New->next = p->next->next;
p->next = New;
return 1;
}
//增
int InsertElem_List(Node*list, int index, int &e)//在第index位置变为元素e
{
Node*p = list->next;
int j = 1;
while (p&&j < index - 1)
{
p = p->next;
j++;
}
if (!p || j > index) return -1;
Node*New = (Node*)malloc(sizeof(Node));
New->data = e;
New->next = p->next;
p->next = New;
return 1;
}
//删
int DeleteElem_List(Node*list, int index, int &e)//删除指定节点e保存其值
{
Node*p = list;
int j = 0;
while (p->next&&j < index - 1)
{
p = p->next;
j++;
}
if (!(p->next || j > index))return -1;
Node *q = p->next;
p->next = q->next;
e = q->data;
delete []q;
}
int main()
{
Node *NodeList;
NodeList=CreateList();
ShowList(NodeList);
//查
int target = 0;
int e = 0;
cout << "请你输入想查询的节点序号" << endl;
cin >> target;
int result = GetElem_List(NodeList, target, e);
cout<<"查询节点的状态"<<result<<" "<<"Value:"<<e<<endl;
//改
int target1 = 0;
cout << "请输入你想改变节点的位置" << endl;
cin >> target1;
int value = 0;
cout << "请输入你想修改的值" << endl;
cin >> value;
InsertElem_List(NodeList, target1, value);
ShowList(NodeList);
//删
int target2 = 0;
cout << "请输入你想删除节点的位置" << endl;
cin >> target2;
int value1 = 0;
DeleteElem_List(NodeList, target2, value1);
cout << "删除的元素为:" << value1 << endl;
ShowList(NodeList);
//增
int target3 = 0;
cout << "请输入你想添加节点的位置" << endl;
cin >> target3;
int value2 = 0;
cout << "请输入你想添加的值" << endl;
cin >> value2;
InsertElem_List(NodeList, target3, value2);
ShowList(NodeList);
system("pause");
return 0;
}
栈(Stack)
//栈(Stack)是限定只能在表尾进行插入和删除操作的线性表
#include<iostream>
using namespace std;
template <class T>
struct StackNode {
T data;
StackNode *next;
};
template<class T>
class myStack {
public:
myStack();
~myStack();
void push(T data); //入栈
void pop(); //出栈
T StackTop();
bool isEmpty();
private:
StackNode<T>*top;
};
template <class T>
myStack<T>::myStack() {
top = NULL;
}
template<class T>
myStack<T>::~myStack() {
StackNode<T>*node = NULL;
while (top != NULL)
{
node = top;
top = top->next;
delete node;
}
}
template<class T>
void myStack<T>::push(T data) {
StackNode<T>*node = new StackNode<T>;
node->data = data;
node->next = top;
top = node;
}
template<class T>
void myStack<T>::pop() {
StackNode<T>*node = top;
top = top->next;
delete node;
}
template <class T>
bool myStack<T>::isEmpty() {
return top == NULL;
}
template<class T>
T myStack<T>::StackTop() {
return top->data;
}
int main()
{
myStack<int>*s = new myStack<int>;
for (int index = 0; index < 10; index++)
{
s->push(index);
cout << index << endl;
}
cout << endl;
while (!s->isEmpty()) {
int i = s->StackTop();
cout << i << endl;
s->pop();
}
system("pause");
return 0;
}
Hanoi Tower
//Hanoi 问题 X Y Z X->Y
#include<iostream>
#include<string>
using namespace std;
void Hanoi(int n, string a, string b, string c) {
//a->c,经过b
if (n == 1)
cout << a << " -> " << c << endl;
else {
Hanoi(n - 1, a, c, b);
Hanoi(1, a, b, c);
Hanoi(n - 1, b, a, c);
}
}
int main() {
int n = 0;
cout << "Please input the number of Hanoi:" << endl;
cin >> n;
string a = "X", b = "Y", c = "Z";
Hanoi(n,a, b, c);
system("pause");
return 0;
}
KMP算法:快速查找字符串
参考大佬的,数据结构里面提到过KMP,我…
//KMP算法
//快速查询字符串S1在S2中的位置
//KMP算法关键之处就是构建next表,完事之后才懂next是自身与自身比较的结果
#include<bits/stdc++.h>
using namespace std;
int Next[10000];
void get_next(char b[])
{
int i = 0, j = -1;
Next[0] = -1;
while (b[i] != '\0')
{
if (j == -1 || b[i] == b[j])
{
i++;
j++;
Next[i] = j;
}
else
j = Next[j];
}
}
/*
abababca
i 0 1 2 3 4
j -1 -1 0 1 2
next -1 0 0 1 1
*/
void Index_KMP(char a[], char b[])
{
get_next(b);
int i = 0, j = 0;
int len1 = strlen(a), len2 = strlen(b);
while (i < len1&&j < len2)
{
if (j == -1 || a[i] == b[j])
{
i++;
j++;
}
else
j = Next[j];
}
if (j >= len2)cout << i - len2 + 1 << endl;
else cout<<"-1" << endl;
}
int main()
{
char a[10000], b[10000];
while (cin >> a >> b)
{
Index_KMP(a, b);
}
system("pause");
return 0;
}
二叉树构造
#include<iostream>
using namespace std;
typedef struct node {
int data;
node* lchild, *rchild;
}BinTNode;
typedef BinTNode * BinTree;
//CreateTree
//广义表----->二叉树 额有点东西 广义表((a),(b,(c,d)))
BinTNode * CreateTree(const char*str)
{
BinTNode *St[100];
BinTNode *p = NULL;
int top, k = 0, j = 0;
top = -1;
char chr = str[j];
BinTNode* b = NULL;
while (chr != '\0')
{
switch (chr) {
case '(':top++; St[top] = p; k = 1; break;
case ')':top--; break;
case ',':k = 2; break;
default:
p = (BinTNode*)malloc(sizeof(BinTNode));
p->data = chr;
p->lchild = p->rchild = NULL;
if (b == NULL) b = p;
else {
switch (k) {
case 1:St[top]->lchild = p; break;
case 2:St[top]->rchild = p; break;
}
}
}
j++;
chr = str[j];
}
return b;
}
//按照完全二叉树的层次顺序建立二叉链表
//@虚节点 #结束符
BinTree CreateBinTree(BinTree bt) {
BinTNode* Q[100];
BinTNode* s;
int front, rear;
char ch; ch = getchar();
bt = NULL;
front = 1;
rear = 0;
while (ch != '#') {
s = NULL;
if (ch!= '@') {
s = (BinTNode*)malloc(sizeof(BinTNode));
s->data = ch;
s->lchild = s->rchild = NULL;
}
rear++;
Q[rear] = s;
if (rear == 1)bt = s;
else {
if (s != NULL && Q[front] != NULL) {
if (rear % 2 == 0) {
Q[front]->lchild = s;
}
else Q[front]->rchild = s; }
if (rear % 2 != 0)
front++;
}
ch = getchar();
}
return bt; }
//前序遍历算法
void Preorder(BinTree bt) {
if (bt != NULL) {
printf("%c", bt->data);
Preorder(bt->lchild);
Preorder(bt->rchild);
}
}
//中序遍历算法
void Midorder(BinTree bt) {
if (bt != NULL) {
Midorder(bt->lchild);
printf("%c", bt->data);
Midorder(bt->rchild);
}
}
//后序遍历算法
void Postorder(BinTree bt) {
if (bt != NULL) {
Postorder(bt->lchild);
Postorder(bt->rchild);
printf("%c", bt->data);
}
}
BubbleSort
//冒泡排序
#include<iostream>
using namespace std;
int main()
{
int a[100], i, j, t, n;
cin>>n; //输入N个数
for (i = 0; i <= n-1; i++) {
cin >> a[i];
}
for (i = 0; i < n-1 ; i++) {
for (j = 0; j < n-1 - i; j++) {
if (a[j] > a[j + 1]) {
t = a[j]; a[j] = a[j +1]; a[j+1 ] = t;
}
}
}
for (i = 0; i <n; i++)
cout<<a[i]<<"<";
system("pause");
return 0;
}
BubbleSortTest
#include<iostream>
using namespace std;
struct student {
int score;
char name[20];
};
int main()
{
student stu[100],t;
int n; //n位同学
cin >> n;
cout << endl;
for (int i = 0; i < n; i++) {
cin >> stu[i].name >> stu[i].score;
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i-1; j++) {
if (stu[j].score < stu[j+1].score) {
t = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = t;
}
}
}
for (int i = 0; i < n; i++) {
cout << stu[i].name << ":" << stu[i].score << endl;
}
system("pause");
return 0;
}
QuickSort
/以数组第一个数为基准,/从右开始找一个比基准小的数,并交换位置,从左边找第一个比基准大的的数,并交换位置,在分别处理基准的两边。
#include<iostream>
using namespace std;
int a[101], n;
void QuickSort(int left, int right) {
int i, j, t, temp;
if (left > right)
return;
temp = a[left];
i = left;
j = right;
while (i != j) {
while (a[j] >= temp && i < j)
j--;
while (a[i] <= temp && i < j)
i++;
if (i < j) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
a[left] = a[i];
a[i] = temp;
QuickSort(left, i - 1);
QuickSort(i + 1, right);
}
int main() {
int n; //输入n个数据
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
QuickSort(0, n - 1);
for (int i = 0; i < n; i++)
cout << a[i] << " <";
system("pause");
return 0;
}
Queue
/*
queue
问题:
给你一组数据,删除第一个数,将第二个数放到数组最后面,删除第三个数,将第四个数放到最后面
直到剩下一个数,也删除。
*/
#include<iostream>
using namespace std;
struct queue {
int data[100];
int head;
int tail;
};
int main() {
queue q;
q.head = 0;
q.tail = 0;
for (int i = 1; i <= 9; i++) {
cin >> q.data[q.tail];
q.tail++;
}
while (q.tail > q.head) {
//输出首项
cout << q.data[q.head];
q.head++;
//将新的首项放到尾部
q.data[q.tail] = q.data[q.head];
q.tail++;
q.head++;
}
system("pause");
return 0;
}
Stack
//判断字符串是不是回文
#include<iostream>
using namespace std;
typedef struct {
char data[1000];
int top;
}Stack;
void InitStack(Stack *s) {
s->top = 0;
}
int PushStack(Stack *s, char ch) {
s->data[s->top] = ch;
s->top++;
return 1;
}
int PopStack(Stack *s, char *ch) {
*ch = s->data[s->top - 1];
s->top--;
return 1;
}
int main() {
char data[1000];
Stack s;
char ch;
InitStack(&s);
cin >> data;
for (int i = 0; i < strlen(data); i++) {
PushStack(&s, data[i]);
}
for (int j = 0; j < strlen(data); j++) {
PopStack(&s, &ch);
if (data[j] != ch) {
cout << "不是回文";
return 0;
}
}
cout << "回文数" << endl;
system("pause");
return 0;
}
StackAndQueue
#include<iostream>
using namespace std;
//接竹竿,双方有有相同张数的扑克牌,轮流放牌,当有相同的牌,将收掉相同之间的牌,直到一方没牌为止
struct queue {
int data[1000];
int head;
int tail;
};
struct stack {
int data[1000];
int top;
};
int main() {
queue q1, q2;
stack s;
int book[10];
q1.head = 1; q1.tail = 1;
q2.head = 1; q2.tail = 1;
s.top = 0;
//用来标记那些牌已经出现在桌面上了
for (int i = 1; i <= 9; i++) {
book[i] = 0;
}
//q1手中的牌
for (int j = 1; j <= 6; j++) {
cin >> q1.data[q1.tail];
q1.tail++;
}
//q2手中的牌
for (int k = 1; k <= 6; k++) {
cin >> q2.data[q2.tail];
q2.tail++;
}
//默认双方的牌1--9
while (q1.head < q1.tail&&q2.head < q2.tail) {
//q1先出牌
int t = q1.data[q1.head];
if (book[t] == 0) {
//q1没有赢牌
q1.head++;
s.top++;
s.data[s.top] = t;
book[t] = 1;
}
else {
//q1赢牌
q1.head++;
q1.data[q1.tail] = t;
q1.tail++;
while (s.data[s.top] != t) {
book[s.data[s.top]] = 0;
q1.data[q1.tail] = s.data[s.top];
q1.tail++;
s.top--;
}
}
t = q2.data[q2.head];
//判断q2能否赢牌
if (book[t] == 0) {
//q2没赢牌
q2.head++;
s.top++;
s.data[s.top] = t;
book[t] = 1;
}
else {
//q2赢牌
q2.head++;
q2.data[q2.tail] = t;
q2.tail++;
while (s.data[s.top] != t) {
book[s.data[s.top]] = 0;
q2.data[q2.tail] = s.data[s.top];
q2.tail++;
s.top--;
}
}
}
if (q2.head == q2.tail) {
cout << "q1win" << endl;
cout << "q1's card" << endl;
for (int i = q1.head; i < q1.tail; i++) {
cout << q1.data[i] << " ";
}
cout << endl;
if (s.top > 0) {
cout << "桌上的牌" << endl;
for (int i = 1; i <= s.top; i++) {
cout << s.data[i]<<" ";
}
}
else {
cout << "桌上没牌了" << endl;
}
}
else {
cout << "q2 win" << endl;
cout << "q2's card" << endl;
for (int i = q2.head; i < q2.tail; i++) {
cout << q2.data[i];
}
if (s.top > 0) {
cout << "桌上的牌\n";
for (int i = 1; i <= s.top; i++) {
cout << s.data[i]<<" ";
}
}
else {
cout << "桌上已经没牌了";
}
}
system("pause");
return 0;
}
demo 数据
2 4 1 2 5 6
3 1 3 5 6 4
Bomberman
//Bomberman
//已知一个地图,有墙(#),炸弹炸不破,有空地(_)安防炸弹,有敌人(G),在哪安装炸弹可以消灭最多的人
#include<iostream>
using namespace std;
int main() {
char map[20][21]; //假设地图是20*20
int row, col; //地图大小
cin >> row >> col;
for (int i = 0; i < row; i++) {
cin >> map[i];
}
int maxKill = 0;
int p, q; //保存炸弹位置
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
//判断是不是平地(__)可以安防炸弹
if (map[i][j]=='_') { //"" 和 ''不一样
int sum = 0; //记录消灭敌人的个数
int x, y;
x = i;
y = j;
//向上查看敌人
while (map[x][y] != '#') {
if (map[x][y] == 'G') {
sum++;
}
x--;
}
//向下
x = i;y = j;
while (map[x][y] != '#') {
if (map[x][y] == 'G') {
sum++;
}
x++;
}
//向右
x = i; y = j;
while (map[x][y] != '#') {
if (map[x][y] == 'G') {
sum++;
}
y++;
}
//向左
x = i; y = j;
while (map[x][y] != '#') {
if (map[x][y] == 'G') {
sum++;
}
y--;
}
if (sum > maxKill) {
maxKill = sum;
p = i;
q = j;
}
}
}
}
printf("将炸弹放咋(%d,%d),炸死的敌人最多为:%d", p, q, maxKill);
system("pause");
return 0;
}
DFS
//DFS深度优先遍历
//有向图寻找最短路径
#include<iostream>
using namespace std;
int min = 9999;
int book[101]; //记录走过的城市,避免死循环
int e[101][101]; //c++默认数组元素为0
int n; //目标城市
void DFS(int cur, int dis) {
if (dis > min)
return;
if (cur == n) {
if (dis < min)min = dis;
return;
}
for (int j = 1; j <= n; j++) {
if (e[cur][j] != 9999 && book[j] == 0) {
book[j] = 1;
DFS(j, dis + e[cur][j]);
book[j] = 0;
}
}
return;
}
int main() {
int m; //有几条路
//输入道路
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
e[a][b] = c;
}
book[1] = 1; //从1开始
DFS(1, 0);
cout << "最短路劲:" << min;
system("pause");
return 0;
}