C++部分内容练习(复习)

练习部分有:const关键字,函数指针,内部类,初始化列表,

STL vector with iterator 和 for_each 算法.

直接看代码,注意看注释.

//VC2005,dev-cpp 4.9.9.2(gcc)下编译通过.

//main.cpp  
#include<iostream>  
#include <string>  
#include <vector>  
#include <algorithm>//for_each  

using namespace std;  

void prints();  
void copy();  
void (*pFun[])() = {prints, copy};//定义一个函数指针数组  

//以下为for_each第三个参数的定义  
template<class T>  
class mFun  
{  
private:  
T fa;  
public:  
mFun(const T &name):fa(name)  
{ }  

void operator()(T &name)const  
{  
name*=fa;  
}  
};   

//以下为双向链表的定义
//暂时只实现了简单的加入和删除,主要是体现一个思想.
template<class Type>  
class LIST  
{  
public:  
//无参数构造函数.  
LIST(){  
head = NULL;  
tail =NULL;  
count = 0;  
}  

//前插入式双向链表  
void Add(Type d){  
//注意处理空链表和有数据的链表时添加结点的问题  
if(head==NULL)  
{  
head = new _node<Type>(d);  
head->setNext(head);  
head->setPrev(head);  
tail = head;  
}else  
{  
_node<Type> * temp = new _node<Type>(d);  
temp->setPrev(head->Prev());  
temp->setNext(head);  
head->setPrev(temp);  
tail->setNext(temp);  
head = temp;  
}  
count++;  
}  

//删除数据为d的节点  
void Delete(Type d)  
{  
int iPos=count;  
for(_node<Type> * p = head; iPos>0; p=p->Next())  
{  
--iPos;  
if(p->Data()==d)  
{  
_node<Type> * tn= p->Next();  
_node<Type> * tp= p->Prev();  
tn->setPrev(tp);  
tp->setNext(tn);  
//如果是头结点,则还应该将头结点head移位  
if(p==head)  
head=head->Next();  
count--;  
delete p;  
return ;  
}  
}//end for  

}  

//打印所有的节点  
void PrintAll()const  
{  
if(head!=NULL){  
cout<<"all data:"<<endl;  
int iPos=count;  
for(_node<Type> * p = head;iPos>0 ;p= p->Next() )  
{  
--iPos;  
cout<<hex<<"0x"<<p->Prev()<<"-<"<<p<<">-"<<p->Next()<<":"<<dec<<p->Data()<<endl;  
}  
cout<<"-----end-----"<<endl;  
}  
}  
virtual ~LIST(){  
if(head!=NULL){  
int iPos = count;  
for(_node<Type> * p = head;iPos>0;)  
{  
--iPos;  
_node<Type> * t = p;  
p= p->Next();  
delete t;  
}  
head = NULL;  
tail    = NULL;  
count = 0;  
}  
cout<<endl<<"list deleted!"<<endl;  
}  
protected:  
private:  
//内部类,由于_node仅仅在LIST类中使用,故无必要定义至外部;
//可以将_node所有成员设置为public,这样,便不必有成员函数了,
//在封装和暴露中间取个平衡点即可,不必死守规则.
template<class T>  
class _node  
{  
public:  
_node(T d):_data(d)  
{
//通过初始化列表来初始化_data,这样效率高.
next = NULL;  
prev = NULL;  
}  
T & Data()  
{  
return _data;  
}  

void setPrev(_node * pr){  
prev = pr;  
}  

_node * Prev()  
{  
return prev;  
}  


void setNext(_node * n){  
next = n;  
}  

_node * Next()  
{  
return next;  
}  


private:  
_node * next;  
_node * prev;  
T _data;  
};  
unsigned int count;  
_node<Type> * head;//头结点  
_node<Type> * tail;//尾结点  
};  

int main()  
{     
cout<<"----------------------------------------------------------------"<<endl;   
cout<<"const关键字练习:"<<endl;   
const char *p1  = "Hello,你好.";//p1可以改变,字符串内容不可改变  
char * const p2 = "imzc";//p2不可改变,但是字符串内容可以  
const char * const p3  = "this is p3.";//都不可改变  
char * t = new char[30];
//只有通过动态分配或者char p[30]这种形式的,指向的内容才可以改变
strcpy(t,p1);  
p1++ ;   
t[1]='a';  
cout<<"p1:"<<p1<<endl<<"p2:"<<p2<<endl<<"p3:"<<p3<<endl<<"t:"<<t<<endl;  
delete [] t;  
cout<<"----------------------------------------------------------------"<<endl;  
cout<<"函数指针练习:"<<endl;   
cout<<">";   
int index=0;  
cin >>index;  
do{   
if(index>2||index<=0)  
{  
cout<<"Only has two function!1,prints;2,copy."<<endl<<">";  
cin>>index;  
}  
else  
{  
pFun[index-1]();  
cout<<"return to main."<<endl;  
}  
} while (index>2||index<=0);  
cout<<"----------------------------------------------------------------"<<endl;  
cout<<"双向链表练习:"<<endl;  
LIST<string> li;  
li.Add("hello");  
li.Add("see");  
li.Add("yes");  
li.Add("no");  
li.Add("3qs");  
li.PrintAll();  
cout<<"Delete: "no"string."<<endl;  
li.Delete("no");  
li.PrintAll();  
cout<<"----------------------------------------------------------------"<<endl;  
cout<<"for_each 实例:"<<endl;  
vector<int> v;  
v.push_back(12);  
for (int i=0;i<100;i+=10)  
{  
v.push_back(i);  
}  
cout<<"原始向量:";  
for (vector<int>::iterator v1=v.begin();v1!=v.end();v1++)  
{  
cout<<*(v1)<<" ";  
}  
cout<<endl;  
for_each ( v.begin ( ) , v.end ( ) , mFun<int> ( 12 ) );  
cout<<"for_each->add(-2):";  
for (vector<int>::iterator v1=v.begin();v1!=v.end();v1++)  
{  
cout<<*(v1)<<" ";  
}  
cout<<endl;   

return 0;  

}  

void prints()  
{  
cout<<"prints() function"<<endl;  
}  

void copy()  
{  
cout<<"copy function"<<endl;  
}
| 279个评论