15
2012
10

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;
}


15
2012
10

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;
}


14
2012
10

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);
}


13
2012
10

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;
}


12
2012
10

linux下练习 c++ 栈中使用类模版例子

code:

//栈中使用类模版例子
#include <iostream>
#include <typeinfo>
using namespace std;
//typedef char T;
template<typename T,int len=4>//可以有默认值,有默认值的参数靠右
//如果成员函数在类外写,那么每个函数前要加语句: template<typename T> 
class Stack
{
T a[len];
int cur;
public:
Stack():cur(0){}
const char* stype()const//类型
{
return typeid(T).name();
}
int max_size()const//最大空间
{
return len;
}
void push(const T& d) throw(const char*)//入栈
{
if(full()) throw "满";
a[cur++]=d;
}
T pop() throw (const char*)//出栈
{
if(empty()) throw "空";
return a[--cur];
}
const T& top() const throw(const char*)//取顶元素
{
if(empty()) throw "空";
return a[cur-1];
}
bool empty()const//是否空
{
return cur==0;
}
bool full()const//是否满
{
return cur==len;
}
void clear()//清空
{
cur=0;
}
void travel()//遍历
{
while(!empty())
{
cout<<pop()<<' ';
}
cout<<endl;
}
int size()const{return cur;}//元素个数
};
int main()
{
Stack<char,10> s;
Stack<int> d;
s.push('+');s.push('-');s.push('*');s.push('/');
d.push(123);
d.push(42);
d.push(666);
cout<<s.stype()<<endl;
s.travel();
cout<<d.stype()<<endl;
d.travel();
return 0;
}


05
2012
10

linux下练习 c++ 几种排序算法

code:

#include <iostream>
#include<algorithm>
#include<ctime>
using namespace std;
void sort(int* 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);
}
void sort1(int* a,int n)//插入排序
{
int j;
for(int i=1;i<n;i++)
{
int t=a[i];
for(j=i;j>0&&t<a[j-1];j--)
a[j]=a[j-1];
a[j]=t;
}
}
void sort2(int* a,int n)//高效冒泡排序
{
int j;
for(int i=0;i<n-1;i++)
{
int t=i;
for(j=i+1;j<n;j++)
if(a[j]<a[t]) t=j;
if(i!=t) swap(a[t],a[i]);
}
}
void sort3(int* a,int n)//快速(分组)排序
{
if(n<=1) return;
else if(n==2)
{
if(a[0]<=a[1]) return;
else swap(a[0],a[1]);
}
else
{
swap(a[n/2],a[0]);
int jie =a[0];
int* L=a+1;
int* R=a+n-1;
while(L<R)
{
while(L<R && *L<jie) ++L;
while(a<R && *R>=jie) --R;
if(L<R) swap(*L,*R);
}
if(*R <jie) swap(*R,a[0]);
sort(a,R-a);
sort(R+1,n-1-(R-a));
}
}
int main()
{
const int N=10240;
int a[N];
for(int i=0;i<N;i++)
{
a[i]=N-i;
}
clock_t t1=clock();
sort(a,N);
clock_t t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
//int b[5]={45,42,67,88,11};
for(int i=0;i<N;i++)//重新赋值
{
a[i]=N-i;
}
t1=clock();
sort1(a,N);
t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
for(int i=0;i<N;i++)//重新赋值
{
a[i]=N-i;
}
t1=clock();
sort2(a,N);
t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
for(int i=0;i<N;i++)//重新赋值
{
a[i]=N-i;
}
t1=clock();
sort3(a,N);
t2=clock();
cout<<"用时:"<<double(t2-t1)/CLOCKS_PER_SEC<<endl;
for(int i=21;i<31;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
}


05
2012
10

linux下练习 c++ 二分查找

code:

#include<iostream>
using namespace std;
class person
{
string name;
int age;
public:
person(const char* n,int a):name(n),age(a){}
friend bool operator <(const person& a,const person& b)//运算符重载-比较年龄大小
{
return a.age<b.age;
}
friend bool operator >(const person& a,const person& b)//运算符重载-比较年龄大小
{
return a.age>b.age;
}
friend bool operator ==(const person& a,const person& b)//运算符重载-等于号
{
return a.age==b.age;
}
friend ostream& operator<<(ostream& o,const person& a)//运算符重载-输出
{
o<<a.name<<":"<<a.age<<endl;
}
};
person* bsearch(person* a,int n,const int age)//二分查找
{
if(n<=0) return NULL;
int mid=n/2;
person p("",age);
if(a[mid]==p) return a+mid;
if(p<a[mid]) return bsearch(a,mid,age);
else return bsearch(a+mid+1,n-mid-1,age);
}
person* bsearch2(person* a,int n,const int age)//二分查找
{
int b=0,e=n-1;
person t("",age);
while(b<=e)
{
int mid=(b+e)/2;
if(a[mid]==t) return a+mid;
if(t<a[mid]) e=mid-1;
else b=mid+1;
}
return NULL;
}
int main()
{
person a[5]={person("a1",34),
person("a2",25),
person("a3",16),
person("a4",77),
person("a5",40)};
for(int i=0;i<5;i++)//排序
{
for(int j=i+1;j<5;j++)
if(a[j]<a[i]) swap(a[j],a[i]);
}
for(int i=0;i<5;i++)
cout<<a[i];
int fage;
cout<<"请输入要查找的年龄:";
cin>>fage;
person* p=bsearch2(a,5,fage);//查找
if(p!=NULL) cout<<*p;
else cout<<"未找到!\n";
}


29
2012
09

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初始化完成…\n";
}
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<<"…内存释放完成…\n";
}
};
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;
}

 

28
2012
09

linux学习与c编程2

  1. cc -S c.c          --编译c.c 文件,会生成 c.s的汇编代码文件  

27
2012
09

linux学习与c编程1

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

  

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

  

查看当前shell 用:ps  

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

退出一个shell 用 exit   

切换shell,如: exec tsh