C++篇——函数、类模板及string类

函数模板

函数模板的作用:一个函数解决各种类型的问题

格式:
template<class 类型参数1, class 类型参数2, … >
返回值类型 模板名 (形参表)
{
函数体
}

函数模板:交换各种类型的变量值

交换两个变量值的函数模板
template <classT>
void Swap(T & x,T & y)
{
T tmp = x;
x = y;
y = tmp;
}
int main(){
int n = 1, m = 2;
Swap(n, m); //编译器自动生成 void Swap(int &, int &)函数
double f = 1.2, g = 2.3;
Swap(f, g); //编译器自动生成 void Swap(double &, double &)函数
return 0;
}

函数模板可以重载, 只要它们的形参表不同即可

C++编译器遵循以下优先顺序:
Step 1: 先找参数完全匹配的普通函数(非由模板实例化
而得的函数)
Step 2: 再找参数完全匹配的模板函数
Step 3: 再找实参经过自动类型转换后能够匹配的普通
函数
Step 4: 上面的都找不到, 则报错
可以在函数模板中使用多个类型参数, 可以避免二义性

类模板

  • 定义一批相似的类
  • 定义类模板
  • 生成不同的类

类模板的定义:

C++的类模板的写法如下:
template <类型参数表>
class 类模板名
{
成员函数和成员变量
};
类型参数表的写法就是:
class 类型参数1, class 类型参数2, …
类模板的定义:
类模板里的成员函数, 如在类模板外面定义时,
template <型参数表>
返回值类型 类模板名<类型参数名列表>::成员函数名(参数表)
{
 ……
}

用类模板定义对象的写法如下:
——类模板名 <真实类型参数表> 对象名(构造函数实际参数表);
如果类模板有无参构造函数, 那么也可以只写:
——类模板名 <真实类型参数表> 对象名;

使用类模板声明对象
编译器由类模板生成类的过程叫类模板的实例化
• 编译器自动用具体的数据类型
->替换类模板中的类型参数, 生成模板类的代码
由类模板实例化得到的类叫模板类
• 为类型参数指定的数据类型不同, 得到的模板类不同

函数模版作为类模板成员

#include <iostream>
using namespace std;
template <class T>
class A{
 public:
template<class T2>
void Func(T2 t) { cout << t; } //成员函数模板
};
int main(){
 A<int> a;
a.Func('K'); //成员函数模板 Func被实例化
return 0;
}

类模板与非类型参数

类模板的参数声明中可以包括非类型参数
template <class T, int elementsNumber>
• 非类型参数: 用来说明类模板中的属性
• 类型参数: 用来说明类模板中的属性类型, 成员操作的参数类型和返回值类型

类模板的 “<类型参数表>” 中可以出现非类型参数:
template <class T, int size>
class CArray{
 T array[size];
public:
 void Print( )
 {
 for(int i = 0; i < size; ++i)
 cout << array[i] << endl;
 }
};
CArray<double, 40> a2;
CArray<int, 50> a3;
注意:
CArray<int,40>和CArray<int,50>完全是两个类
这两个类的对象之间不能互相赋值

类模板——模板类——普通类进行派生。

String类

string 类 是一个模板类, 它的定义如下:

 typedef basic_string<char> string;
使用string类要包含头文件 <string>
string对象的初始化:
• string s1("Hello"); // 一个参数的构造函数
• string s2(8, ‘x’); // 两个参数的构造函数
• string month = “March”; 
························分割线··························
不提供以字符和整数为参数的构造函数
错误的初始化方法:
• string error1 = ‘c’; // 错
• string error2(‘u’); // 错
• string error3 = 22; // 错
• string error4(8); // 错
可以将字符赋值给string对象
• string s;
s = ‘n’;

string类程序样例

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[ ]){
string s1("Hello");
cout << s1 << endl;
string s2(8, 'x');
cout << s2 << endl;
string month = "March";
cout << month << endl;
string s;
s='n';
cout << s << endl;
return 0;
}
/*
程序输出: 4
Hello
xxxxxxxx
March n
*/

string类的用法:

构造的string太长而无法表达-> 会抛出length_error异常
string 对象的长度用成员函数 length()读取;

• string s(“hello”);
cout << s.length() << endl;
string 支持流读取运算符
• string stringObject;
cin >> stringObject;
string 支持getline函数
• string s;
getline(cin, s);

string的赋值和连接

用 ‘=’ 赋值
• string s1("cat"), s2;
s2 = s1;
用 assign成员函数复制
• string s1("cat"), s3;
s3.assign(s1);
**用 assign成员函数部分复制**
• string s1("catpig"), s3;
s3.assign(s1, 1, 3); //从第一个开始复制3个
单个字符复制
s2[5] = s1[3] = ‘a’;
逐个访问string对象中的字符
string s1("Hello");
for(int i=0; i<s1.length(); i++)
 cout << s1.at(i) << endl;
 成员函数at会做范围检查, 如果超出范围, 会抛出out_of_range异常, 而下标运算符不做范围检查
 ------------------------------------------
 用 + 运算符连接字符串
 string s1("good "), s2("morning! ");
 s1 += s2;
 cout << s1;
用成员函数 append 连接字符串
 string s1("good "), s2("morning! ");
 s1.append(s2);
 cout << s1;
 s2.append(s1, 3, s1.size()); //s1.size(), s1字符数
 cout << s2;
 //下标为3开始, s1.size()个字符
//如果字符串内没有足够字符, 则复制到字符串最后一个字符

用关系运算符比较string的大小

• == , >, >=, <, <=, !=可以直接进行比较
• 返回值都是bool类型, 成立返回true, 否则返回false

string s1("hello"), s2("hello"), s3("hell");
 bool b = (s1 == s2);
 cout << b << endl;	//1
 b = (s1 == s3);
 cout << b << endl;	//0
 b = (s1 > s3);
 cout << b << endl;	//1

用成员函数compare比较string的大小

string s1("hello"), s2("hello"), s3("hell");
int f1 = s1.compare(s2);
int f2 = s1.compare(s3);
int f3 = s3.compare(s1);
int f4 = s1.compare(1, 2, s3, 0, 3); 				//s1 1-2; s3 0-3
int f5 = s1.compare(0, s1.size(), s3); 				//s1 0-end
cout << f1 << endl << f2 << endl << f3 << endl;
cout << f4 << endl << f5 << endl; 
/*
0 // hello == hello
 1 // hello > hell
-1 // hell < hello
-1 // el < hell
 1 // hello > hell
 */

string类的子串与交换函数

子串:
成员函数 substr()
string s1("hello world"), s2;
s2 = s1.substr(4,5); //下标4开始5个字符
cout << s2 << endl;		//o wor
·····································
交换:
成员函数 swap()
string s1("hello world"), s2("really");
s1.swap(s2);
cout << s1 << endl;		//really
cout << s2 << endl;		//hello world

string其他成员函数

  • 成员函数length()和size()相同返回字符串的大小/长度
  • 成员函数empty()返回string对象是否为空
  • 成员函数resize()改变string对象的长度

寻找string中的子串

成员函数 find()
• string s1("hello world");
s1.find("lo");
//在s1中从前向后查找 “lo” 第一次出现的地方
//如果找到, 返回 “lo”开始的位置, 即 l 所在的位置下标
//如果找不到, 返回 string::npos (string中定义的静态常量)
成员函数 rfind()
• string s1("hello world");
s1.rfind("lo");
//在s1中从后向前查找 “lo” 第一次出现的地方
//如果找到, 返回 “lo”开始的位置, 即 l 所在的位置下标
//如果找不到, 返回 string::npos
成员函数 find_first_not_of()
• string s1("hello world");
s1.find_first_not_of(“abcd");
//在s1中从前向后查找不在 “abcd” 中的字母第一次出现的地方
//如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
成员函数 find_last_not_of()
• string s1("hello world");
s1.find_last_not_of(“abcd");
//在s1中从后向前查找不在 “abcd” 中的字母第一次出现的地方
//如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos

替换string中的字符

成员函数erase()
 string s1("hello worlld");
 s1.erase(5);
 cout << s1;
 cout << s1.length();
 cout << s1.size();
// 去掉下标 5 及之后的字符
替换string中的字符
输出:hello55
---------------------------------
成员函数 find()
 string s1("hello worlld");
 cout << s1.find("ll", 1) << endl;
cout << s1.find("ll", 2) << endl;
 cout << s1.find("ll", 3) << endl;
 //分别从下标1, 2, 3开始查找 “ll”
 ---------------------------------
 成员函数 replace()
 string s1("hello world");
 s1.replace(2,3, "haha", 1,2);
 cout << s1;
 //将s1中下标2 开始的3个字符
 //换成 “haha” 中下标1开始的2个字符
替换string中的字符
输出:heah world

在string中插入字符

成员函数 insert()
 string s1(“hello world”);
 string s2(“show insert”);
 s1.insert(5, s2); // 将s2插入s1下标5的位置
 cout << s1 << endl;		helloshow insert world
 s1.insert(2, s2, 5, 3);
 //将s2中下标5开始的3个字符插入s1下标2的位置
 cout << s1 << endl;		heinslloshow insert world

转换成C语言式char * 字符串
成员函数 c_str()
string s1(“hello world”);
printf("%s\n", s1.c_str());
// s1.c_str() 返回传统的const char * 类型字符串
//且该字符串以 ‘\0’ 结尾

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值