您的位置:首页 > 编程语言 > C语言/C++

C++ STL之命名空间、函数模板、类模板

2016-12-04 12:19 447 查看
1 命名空间

#include <iostream.h>
//using namespace std;

namespace name1
{
int max(int a,int b)
{
int result= a>b? a:b;
return result;
}
}

namespace name2
{
int max(int a,int b)
{
int result= a>b? a:b;
cout<<"the max is "<<result<<endl;
return result;
}

}

using namespace name1;
using namespace name2;

int main()
{

cout<<name2::max(10,20)<<endl;
return 0;
}

注意点:
1、main函数前可以不加
using namespace name1;
using namespace name2;
那么,使用name1或name2下的函数(或类)时,就必须在函数前 加 作用域

2、头文件一样不能省
但一般使用using namespace name1这样的语句来声明 函数、类的原型

2 函数模板
代码:
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

template <class T>
T max(T a,T b)
{
T result= a>b? a:b;
return result;
}

/*
float max(float a,float b)
{
float result= a>b? a:b;
return result;
}

char max(char a,char b)
{
char result= a>b? a:b;
return result;
}*/

int main()
{

//cout<<max('d','c')<<endl;
cout<<max(100,167)<<endl;

return 0;
}

知识点:
1、函数参数,如果是 字符串-------实际上是 char *
2、strcmp
字符串的比较函数------C语言
比较的是 2个字符串的 ASCII
靠返回值来判断谁大 谁小
a-b
3、比较汉字的大小
实际上比较的是汉字的 GBK编码 大小
但该编码顺序,刚好和 新华字典 顺序一致--------根据拼音

4、如果调用某个函数时,发现,既有 函数模板,也有 普通函数------------普通函数 优先级高,首先调用

#include <iostream.h>
#include <stdlib.h>
#include <string.h>

template <class T>
T max(T a,T b) //这是函数模板,优先级低,因此当调用max("李四","张三")时,实际调用的是不是该函数模板
{ //在mian中调用max('d','e')时,没有普通函数,因此该函数模板被实际调用
T result= a>b? a:b;
return result;
}

char * max(char * a,char * b) //这是普通函数,优先级高,因此当调用max("李四","张三")时,实际调用的是本函数
{
char * result;
int flag=strcmp(a,b);
if(flag>=0)
{
result=a;
}
else
{
result=b;
}
return result;
}

/*
float max(float a,float b)
{
float result= a>b? a:b;
return result;
}

char max(char a,char b)
{
char result= a>b? a:b;
return result;
}*/

int main()
{

//cout<<max('d','e')<<endl;
cout<<max("李四","张三")<<endl; //GBK GB2312
return 0;
}

类模板

例子:将CStudent类,改造为1个 类模板
改造步骤:
1、将普通类的 int 改为 T
2、在 类声明前加 template <class T>
3、在 成员函数定义前加 template <class T>
4、在 成员函数定义中:作用域后加 <T>
5、在main函数中,定义CStudent类的对象时,在类名后加 <int>-------当number为int时
在类名后加 <char *>----当number为char *时(即,学号可以为字符串,如"First"、"Second")

#include <iostream.h>
#include <stdlib.h>
#include <string.h>

template <class T>
class CStudent
{
public:
T number;
char *name;
CStudent(T number,char *name);

};

template <class T>
CStudent<T>::CStudent(T number,char *name)
{
this->number=number;
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
}

int main()
{
CStudent<int> stu1(1001,"zhangsan");
CStudent<char *> stu2("first","lisi");
cout<<stu2.number<<" "<<stu1.name<<endl;
return 0;
}

3 string
优点:
1、可代替 char *
2、像数组,里面也有元素,且元素类型为 char
3、是 动态数组----即,数组元素可以任意添加和删除,而数组大小会动态进行增大或减小
4、可以直接进行赋值运算,而且是 深复制,不需要使用new和strcpy进行赋值了
5、可以直接使用cin cout
6、使用+可以进行字符串的连接

#include <iostream>
#include <string>

using namespace std;

int main()
{

string str1="zhangsan";
//char *str1="zhangsan";
//char str1[]="zhangsan";
cout<<str1<<endl;
cout<<"请输入1个字符串:";
cin>>str1;
cout<<"您输入的是:"<<str1<<endl;

return 0;
}

例子:---------在CStudent类中,使用string来代替char *
//Student.h
#include <string>
using namespace std;

class CStudent
{
public:
int number;
string name;
CStudent();
CStudent(int number,string name);
void print_student();
virtual ~CStudent();

};

//Student.cpp
CStudent::CStudent(int number,string name)
{
this->number=number;
this->name=name;
}

void CStudent::print_student()
{
cout<<number<<" "<<name<<endl;
return;
}

//main.cpp
#include <iostream>
#include <string>
#include "Student.h"

using namespace std;

int main()
{
CStudent stu1(1001,"zhangsan");
stu1.name=stu1.name+" hello";
stu1.print_student();
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++
相关文章推荐