C++ vector(可变长的动态数组)
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
std::vector<int> myvector;
int a[10] = { 80,900,1000,1,2,3,4,5,6,7};
for(int i = 0; i < 10; ++i) {
myvector.push_back(a[i]);
}
for (size_t j=0; j<myvector.size(); ++j)
{
cout << myvector[j] << endl;
}
sort(myvector.begin(), myvector.end());
for (size_t k = 0; k < myvector.size(); ++k)
{
cout << myvector[k] << endl;
}
vector<int>::iterator result = find(myvector.begin(), myvector.end(), 7);
if (result == myvector.end())
{
cout << "No!" << endl;
}
else
{
cout << "Yes!" << endl;
}
system("pause");
return 0;
}
C++实现矩阵乘法
#include<iostream>
#include<vector>
using namespace std;
vector<vector<int>> matrix_multiply(vector<vector<int>> arrA, vector<vector<int>> arrB)
{
//矩阵arrA的行数
int rowA = arrA.size();
//矩阵arrA的列数
int colA = arrA[0].size();
//矩阵arrB的行数
int rowB = arrB.size();
//矩阵arrB的列数
int colB = arrB[0].size();
//相乘后的结果矩阵
vector<vector<int>> res;
if (colA != rowB)//如果矩阵arrA的列数不等于矩阵arrB的行数。则返回空
{
return res;
}
else
{
//设置结果矩阵的大小,初始化为为0
res.resize(rowA);
for (int i = 0; i < rowA; ++i)
{
res[i].resize(colB);
}
//矩阵相乘
for (int i = 0; i < rowA; ++i)
{
for (int j = 0; j < colB; ++j)
{
for (int k = 0; k < colA; ++k)
{
res[i][j] += arrA[i][k] * arrB[k][j];
}
}
}
}
return res;
}
int main(void)
{
vector<vector<int>> arrA = { { 2, 1 }, { 4, 3 } };
vector<vector<int>> arrB = { { 1, 2 }, { 1, 0 } };
//vector<vector<int>> arrA = { { 1, 2, 3 }, { 4, 5, 6 } };
//vector<vector<int>> arrB = { { 1, 4 }, { 2, 5 }, { 3, 6 } };
vector<vector<int>> res = matrix_multiply(arrA, arrB);
//矩阵arrA的行数
int rowA = arrA.size();
//矩阵arrA的列数
int colA = arrA[0].size();
//矩阵arrB的列数
int colB = arrB[0].size();
for (int i = 0; i < rowA; ++i)
{
for (int j = 0; j < colB; ++j)
{
cout << res[i][j] <<" ";
}
cout << endl;
}
system("pause");
return 0;
}
C++实现双向链表
#include<iostream>
using namespace std;
/*双链表结构*/
typedef struct node
{
int data;
struct node *prior;
struct node *next;
}DNode;
/*创建一个带头节点的双链表*/
void CreateDLink(DNode *&head)
{
int x;
DNode *p, *s;
s = (DNode *)malloc(sizeof(DNode));
if (s == NULL)
{
cout << "Fail to malloc the head node!" << endl;
}
s->data = 0;
s->prior = NULL;
s->next = NULL; //建立一个头结点为head的双链表
head = s; //若是循环双向链表则为:s->next=s->prior=s;
cout << "please input the data of the node" << endl;
cin >> x;
while (x != 0)
{
p = (DNode *)malloc(sizeof(DNode));
if (p == NULL)
{
cout << "Fail to malloc a new node!" << endl;
}
p->data = x;
s->next = p;
p->prior = s;
p->next = NULL;
s = p;
cin >> x;
}
}
void PrintLink(DNode *head)
{
if (head->next == NULL)
{
cout << "The list is NULL" << endl;
return;
}
DNode *p;
p = head->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int Length(DNode *head) //求双链表长度
{
int len = 0;
DNode * p = head->next;
while (p != NULL)
{
len++;
p = p->next;
}
return len;
}
DNode * Get(DNode *head, int i) //获取链表第i个节点地址
{
int j = 0;
DNode *p = head->next;
while (j < i&&p != NULL)
{
p = p->next;
j++;
}
if (p != NULL)
return p;
else
return NULL;
}
int InsertNode(DNode *head, int x, int i)//在第i个位置插入一个节点
{
DNode *s, *p;
s = (DNode *)malloc(sizeof(DNode));
s->data = x;
if (i == 0)
{
s->next = head->next;
if (head->next != NULL)
head->next->prior = s;
s->prior = head;
head->next = s;
}
else
{
p = Get(head, i - 1); //查找待插入节点前一个位置
if (p == NULL)
return 0;
else
{
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
s->prior = p;
p->next = s;
}
}
return 1;
}
void DeleteNode(DNode *head, int index) //删除第index个节点
{
if (head->next == NULL)
{
cout << "The list is NULL" << endl;
return;
}
DNode *p, *s/*保存待删除的节点*/;
if (index == 0)
{
s = head->next;
head->next = s->next;
if (s->next != NULL)
s->next->prior = head;
free(s);
}
else
{
p = Get(head, index - 1);
if (p == NULL)
cout << "The Node " << index << "is not exist" << endl;
else
{
s = p->next;
p->next = s->next;
if (s->next != NULL)
s->next->prior = p;
free(s);
}
}
}
int main(int argc, char *argv[])
{
DNode *head;
CreateDLink(head);
cout << "链表长度为:" << Length(head) << endl;
PrintLink(head);
cout << "第3个节点的值为:" << Get(head, 3)->data << endl;
cout << "输入插入节点的位置及值:" << endl;
int value, index;
cin >> index >> value;
InsertNode(head, value, index);
cout << "插入后的链表为:" << endl;
PrintLink(head);
cout << "请输入要删除的节点位置:" << endl;
cin >> index;
DeleteNode(head, index);
PrintLink(head);
return 0;
}