系统环境linux
编译器GCC
数据库mysql
(编译还需要安装mysql的接口libmysqlclient-dev这个库)
这样的情况下只需要包含<mysql/mysql.h>
然后做以下 *** 作
MYSQL *mysql_init(MYSQL *mysql)//初始化mysql
MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)//连接数据库 具体参数含义请查MYSQL API文档
int mysql_query(MYSQL *mysql, const char *stmt_str)//执行sql语句 如果想添加表的话 stmt_str赋值为"CREATE TABLE XXXX"就行了
下面是将文件 data.txt 中的整数读到一个 vector (相当于一个动态数组)中的例子。
#include <iostream>#include <fstream>
#include <vector>
using namespace std
int main()
{
ifstream fin("data.txt")
vector<int> vec
while (!fin.eof()) {
int val = 0
fin >> val
vec.push_back(val)
}
for (vector<int>::iterator it = vec.begin() it < vec.end() it++) {
cout << *it << " "
}
cout << endl
system("pause")
return 0
}
data.txt 文件内容
运行结果:
链表文件写入与读出,代码示例一:
#include<stdio.h>
struct student
{
int number
char name
struct student*next
}
void save()
void print()
int main()
{
struct student *head=(struct student *)malloc(sizeof(struct student))
head->number=2
head->name='a'
save(head)
print(head)
getch()
}
void save (struct student *head)
{
FILE *p=fopen("d:\\txt.txt","wb")
fwrite(head,sizeof(struct student),1,p)
fclose(p)
}
void print()
{
struct student *head
FILE *p=fopen("d:\\txt.txt","rb")
fread(head,sizeof(struct student),1,p)
fclose(p)
printf("%d\n",head->number)
printf("%c\n",head->name)
}
再提供一个关于动态链表的例子,该程序实现以下功能:
1. initlist_l(lnode&L) //建立一个带头结点的空链表
2. locationelem(linklist L, int i, elemtype &e) //在带头结点的单链表中查找第i个节点,若存在,则返回第i个元素的
值;否则返回error
3. listinsert_l(linklist &L, ini i, elemtype e) //在带头结点的单链表中第i个结点之前插入元素e
4. listdelete_l(linklist &L, int i, elemtype &e) //删除带头结点的单链表L上的第i个数据结点并用e返回其值
5. createlist_l(linklist &L, int n) //通过头插法建立一个含n个结点的单链表
6. createlist_lt(linklist &L, int n) //通过尾插法建立一个含n个结点的单链表
7. display_l(linklist L) //打印链表
#include <stdio.h>#include <malloc.h>
#include <stdlib.h>
typedef int elemtype //用户自定义数据类型
typedef struct lnode
{
elemtype data
struct lnode *next
}lnode, *linklist //结点类型
void initlist_l(linklist &L)
void createlist_l(linklist &L, int n)
void display_l(linklist L) //打印链表
void locationelem(linklist L, int i, elemtype &e)
void listinsert_l(linklist &L, int i, elemtype e)
void listdelete_l(linklist &L, int i, elemtype &e)
void destroy_l(linklist &L) //销毁链表
int main(void)
{
elemtype e = 3, x, insertdata
int num, insertnum, deletenum, len
linklist L
initlist_l(L) //通过函数分配内存空间
printf("输入链表的长度:")
scanf("%d", &len)
createlist_l(L, len) //建立含len个结点的链表
display_l(L)
printf("输入要显示的结点序号:")
scanf("%d", &num)
locationelem(L, num, e)
printf("%d\n", e)
printf("输入插入的位置:")
scanf("%d", &insertnum)
printf("插入的数字:")
scanf("%d", &insertdata)
listinsert_l(L, insertnum, insertdata) //将insertdata插入链表的第insertnum结点之前
display_l(L)
printf("输入删除的结点序号:")
scanf("%d", &deletenum)
listdelete_l(L, deletenum, e)
printf("删除结点的数据域是:%d\n", e)
display_l(L)
destroy_l(L)
return 0
}
void initlist_l(linklist &L)
{
L = (linklist)malloc(sizeof(lnode))
if (L == NULL)
exit(1)
L->next = NULL
}
void locationelem(linklist L, int i, elemtype &e)
{
linklist p = L //p指向表头
int j = 0 //j的初值为0, 即为头结点的序号
while (j<i && p!=NULL)
{
p = p->next
j++ //后移指针并计数
}
if (p == NULL || j > i)
{
printf("该结点不存在\n")
return
}
e = p->data
}
void createlist_l(linklist &L, int n)
{//头插法
int i
linklist s //这里相当于 lnode *s
if (L == NULL)
exit(3)
printf("输入%d个整数:\n", n)
for (i=0 i<n i++)
{
s = (linklist)malloc(sizeof(lnode))
if (s == NULL) //判断s分配空间是否成功
exit(4)
scanf("%d", &s->data)
s->next = L->next
L->next = s
}
}
void listdelete_l(linklist &L, int i, elemtype &e)
{
int j = 0
linklist p, r = L
while (r->next && j<i-1)
{//查找第i-1个节点
r = r->next
j++
}
if (r->next == NULL || j>i-1)
{
printf("链表中没有结点i\n")
return
}
p = r->next
r->next = p->next
free(p)
}
void listinsert_l(linklist &L, int i, elemtype e)
{
linklist s, r = L
int j = 0
while (r->next && j<i-1)
{//找到第i-1个结点
r = r->next
j++
}
if (r == NULL || j>i-1) //老老实实第写r == NULL, 不要刷帅写!r(很容易出错)
{
printf("链表的结点数小于i-1,或i<1\n")
return
}
s = (linklist)malloc(sizeof(lnode))
if (s == NULL)
exit(4)
s->data = e
s->next = r->next
r->next = s
}
void display_l(linklist L)
{
printf("当前链表值为:\n")
linklist p = L->next
while (p)
{
printf("%d ", p->data)
p = p->next
}
printf("\n")
}
void destroy_l(linklist &L)
{//一个一个地删除结点
linklist p = L->next
while (p)
{
L->next = p->next
free(p)
p = L->next
}
free(L)
printf("链表已销毁\n")
}
尾插法:
{
linklist r = L, s
int i
while (r->next)
r = r->next
for (i=0 i<n i++)
{
s = (linklist)malloc(sizeof(lnode))
if (s == NULL)
exit(5)
scanf("%d", &s->data)
s->next = r->next
r->next = s
r = s
}
}
注:来源均来自百度...
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)