真爱无限的知识驿站

学习积累技术经验,提升自身能力

linux下练习 c++ 容器的deque的特性

code:

/*
deque特性
下标:.operator[](i)不检查越界,.at(i) 
删除:.pop_front(),.pop_back()
*/
#include<iostream>
using namespace std;
#include<deque>
#include "print.h"
int main()
{
deque<char> dq;
dq.push_back('c');
dq.push_back('d');
dq.push_back('e');
dq.push_back('f');
print(dq.begin(),dq.end());
dq[1]='t';//把 d 改为 t
for(int i=0;i<dq.size();i++)
cout<<dq[i]<<' ';
cout<<endl;
dq.pop_back();//删除最后一个
dq.pop_front();//删除第一个
print(dq.begin(),dq.end());
return 0;
}


linux下练习 c++ 容器的vector的特性

//vector.cpp

/*
vector的特性
当前容量:.capacity()
约定容量:.reserve()
下标:.operator[](i) ,.at(i) 越界抛出异常
*/
#include<iostream>
using namespace std;
#include<vector>
#include<exception>
#include<typeinfo>
#include "print.h"
void print(const vector< vector<int> >& v)//相当于二维数组
{
for(int i=0;i<v.size();i++)
{
for(int j=0;j<v[i].size();j++)
cout<<v[i][j]<<' ';
cout<<endl;
}
}
int main()
{
vector<double> vt,vt2;//默认容量成倍增长,4,8,16……
for(int i=1;i<10;i++)
{
vt.push_back(i+0.2);
cout<<vt.size()<<'/'<<vt.capacity()<<' ';
}
cout<<endl;
vt2.reserve(9);//只分配这么多,不成倍增长
for(int i=1;i<10;i++)
{
vt2.push_back(i+0.3);
cout<<vt2.size()<<'/'<<vt2.capacity()<<' ';
}
cout<<endl;
vt.at(3)=30.50;//修改值
vt[4]=40.70;
try
{
for(int i=0;i<vt.size();i++)
cout<<vt.at(i)<<' ';
cout<<endl;
}
catch(exception& e)
{
cout<<"
异常:"<<e.what()<<endl;
cout<<"类型:"<<typeid(e).name()<<endl;
}
int m=3,n=5;
vector< vector<int> > vvi(m,vector<int>(n));//二维vector
vvi.resize(m+3);
vvi[1].assign(9,3);
vvi[5].assign(4,5);
print(vvi);
}
/*
template <typename T>
void show(T a[],int n);
template <typename T>
void show(const vector<T>& vt);
*/


linux下练习 c++ 序列容器的使用

code:

// sequence.cpp 
/*
序列式容器:vector,deque,list
插入:.insert(position,n,element), .insert(position,pos_begin,pos_end)
赋值:.assign(n,element), .assign(pos_begin,pos_end)
调整:.resize(n,element=value)
首尾:.front(),.back()
增删:.push_back(element), .pop_back()----删除,返回void
*/
#include <iostream>
#include <deque>
#include <string>
#include "print.h"
using namespace std;
int main()
{
deque<string> ds;
ds.push_back("zhang");//增加
ds.push_back("pu");
ds.push_back("yang");
ds.push_back("xie");
ds.insert(ds.begin()+1,1,"wang");//插入
string s1[3]={"liao","hu","liu"};
ds.insert(----ds.end(),s1,s1+3);
print(ds.begin(),ds.end(),',');
ds.pop_back(); //删除最后一个
ds.pop_back();
print(ds.begin(),ds.end(),',');
ds.resize(10,"pkm");//大小设为10,后面用pkm填充
print(ds.begin(),ds.end(),',');
ds.assign(5,"kkkkk");//5个,都为kkkkk
print(ds.begin(),ds.end(),',');
return 0;
}


linux下练习 c++ 普通容器的使用

code:

/*
迭代器
.begin(),.end() ,.rbegin() , .rend()
插入:.insert(position,element)
删除:.erase(position),.erase(pos_begin,pos_end)
清除:.clear()
大小:.size(), .max_size(), .empty()
交换:.swap(c2) , swap(c1,c2)
运算符:=,>,<,>=,<=,==,!=
*/
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#ifndef print_fun
#define print_fun
template<typename T>
///显示序列数据
void print(T b,T e)
{
bool isExit=false;
while (b!=e)
{
cout<<*b++<<' ';
isExit=true;
}
if(isExit) cout<<endl;
}
#endif
int main()
{
int a[5]={44,33,55,66,11};
vector<int> vi(a,a+5);//用数组初始化一个迭代器vi
cout<<vi.size()<<endl;
sort(vi.begin(),vi.end());//元素排序
vector<int>::iterator b= vi.begin();
print(vi.begin(),vi.end());//正向迭代器
print(a,a+5);
print(vi.rbegin(),vi.rend());//反向迭代器
vi.insert(vi.begin()+1,48);//插入到第2
vi.insert(vi.end(),49);//插入到最后
vi.insert(vi.end()-1,57);
print(vi.begin(),vi.end());
vi.erase(vi.end()-1);//删除最后一个
print(vi.begin(),vi.end());
vi.erase(vi.begin()+2,vi.end()-2);//删除一个区间
print(vi.begin(),vi.end());
vector<int> v2(a,a+5);
vi.swap(v2);//vi与v2交换
print(vi.begin(),vi.end());
vi.swap(v2);//vi再与v2交换回去 
print(vi.begin(),vi.end());
vi.clear();//清空容器
cout<<vi.size()<<endl;
return 0;
}


linux下练习 c++ 库函数排序使用举例

code:

//使用库函数排序举例
#include <iostream>
#include <string>
#include <algorithm>//内有排序库函数
using namespace std;
#ifndef person_h_1  //预定义指令
#define person_h_1
class person
{
public:
person(const char* name,int age):name(name),age(age){}//构造函数
friend ostream& operator<<(ostream& o,const person& p)//重载输出
{
return o<<p.name<<":"<<p.age<<' ';
}
friend bool operator <(const person& a,const person& b)//重载小于
{
return a.age<b.age;
}
private:
string name;
int age;
};
#endif
template<typename T>
void print(T b,T e)//输出数组内容
{
bool isnull=true;
while (b!=e)
{
cout<<*b++<<' ';
isnull=false;
}
if(isnull==false) cout<<endl;
}
int main()
{
int a[6]={5,8,6,4,9,1};
double b[4]={4.4,3.2,6.7,1.2};
string c[5]={"yeah","are","you","people","good"};
person p[3]={person("ppp",23),person("kkk",21),person("mmm",20)};
sort(a,a+6);//排序
sort(b,b+4);
sort(c,c+5);
sort(p,p+3);
print(a,a+6);//输出
print(b,b+4);
print(c,c+5);
print(p,p+3);
}


linux下练习 c++ 函数模版例子

code:

//函数模版使用
//函数模版标准不支持参数默认值
#include<iostream>
#include<cstring>
using namespace std;
template <typename T>
void sort(T* a,int n)//普通冒泡排序
{
bool changed;
do
{
changed=false;
for(int i=1;i<n;i++)
{
if(a[i]<a[i-1])
{
swap(a[i],a[i-1]);
changed=true;
}
}
--n;
}
while(changed);
}
template <>//模版特化
void sort(const char* a[],int n)//普通冒泡排序
{
bool changed;
do
{
changed=false;
for(int i=1;i<n;i++)
{
if(strcmp(a[i],a[i-1])<0)
{
swap(a[i],a[i-1]);
changed=true;
}
}
--n;
}
while(changed);
}
//template <typename T>
//void show(T t[],int n)
template <typename T,int n>
void show(T(&t)[n])
{
//int n=sizeof(t)/sizeof(t[0]);//算出t的个数
for(int i=0;i<n;i++)
cout<<t[i]<<' ';
cout<<endl;
}
template <typename T>
void show(T t)
{
cout<<t<<endl;
}
int main()
{
int a[5]={6,7,8,3,2};
sort(a,5);//函数模版会自动匹配,不需要显式指定类型
show(a);
double d=12.345;
show(d);
char c[5]={'b','f','k','d','a'};
sort(c,5);
show(c);
const char* ca[3]={"fc","ca","ab"};
sort(ca,3);
show(ca);
return 0;
}


linux下练习 c++ 链表操作

code:

#include <iostream>
using namespace std;
typedef int T;
class List
{
struct Node
{
T data;
Node * next;
Node(const T&d=T()):data(d),next(0){}
};
Node * head;
public:
List():head(NULL)
{
cout<<"…list初始化完成…
";
}
void push_front(const T&d)//前插法
{
Node * p=new Node(d);
p->next=head;
head=p;
}
void push_behand(const T&d)//尾插法
{
Node *ph=head;
while(ph->next!=NULL) ph=ph->next;
Node * p=new Node(d);
ph->next=p;
}
void insert(const T&d,int pos)//任意位置插入
{
Node * p=new Node(d);
Node *& pn=getptr(pos);
p->next=pn;
pn=p;
/*
if(pos<=0)
{
    p->next=head;
    head=p;
}
else
{
Node * ph=head;
while(pos-->1 && ph->next !=NULL)
{
ph=ph->next;
}
p->next=ph->next;
ph->next=p;
}
*/
}
void remove(int pos)
{
if(pos<0) return ;
Node *& pn=getptr(pos);
Node *p=pn;
pn=pn->next;
delete p;
}
Node *& getptr(int pos)
{
if(pos<=0) return head;
Node * p =head;
while(pos-->1 && p->next !=NULL)
p=p->next;
return (*p).next; 
}
void travel()const//遍历
{
Node * p=head;
while(p!=NULL)
{
cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
}
void clear()//清空内存
{
while(head != NULL)
{
Node *p=head->next;
delete head;
head=p;
}
}
~List()
{
clear();
cout<<"…内存释放完成…
";
}
};
int main()
{
List l;
l.push_front(5);
l.push_front(4);
l.push_front(7);
l.push_front(6);
l.push_behand(45);
l.push_behand(48);
l.insert(100,0);//小于0位置就插入到最前
l.insert(99,6);//插入到指定位置
l.insert(991,100);//大于最后位置就插入到最后
l.remove(1);
l.remove(0);
l.travel();
return 0;
}

 

linux学习与c编程2

    linux学习与c编程1

    sudo 在没有权限时,运行在命令前。  

      

    运行文件要用文件绝对路径,当前路径 要用 ./文件名   

      

    查看当前shell 用:ps  

    进入另一个 shell,直接输入shell的名字,如:ksh/tcsh/sh/bash/  

    退出一个shell 用 exit   

    切换shell,如: exec tsh   

    拓扑排序 C++实现

    描述:

    学校领导在大学生培养计划的制订中,涉及到课程的安排问题,由于课程较多,现在要求编程高手的你帮忙。假定课程之间的先修关系已确定,现在要求你根据先修关系,通过编程确定各门课程的先后关系,并生成一张课程先后安排顺序表,以帮助学校设置各年度课程。

    输入:

    输入只包括一个测试用例,第一行为一个自然数n,表示课程数量,第二行为n个单词,分别表示n门课程,一个单词表示一门课程,单词全为小写状态,各单词之间用一个空格隔开。

    接下来为n*n行0和1构成的矩阵,表示各门课程之间的先修关系。假设i行j列为1,表示第i课程为第j课程的先修课。否则表示不存在先修关系。

    << < 1 2 3 4 5 6 7 > >>

    Powered By Z-BlogPHP 1.7.3

    Copyright 2024-2027 pukuimin Rights Reserved.
    粤ICP备17100155号