模板和泛型编程

1.模板

函数模板

c++使用模板可以参数多态化,可以不指定确切的数据类型来实现函数和类的通用实现,提高了代码重用性

template 模板的关键字

typename 后面的参数代表任意类型

#include 
using namespace std;
//交换两个整数 mySwap
//void mySwap(int& a,int &b){
//    int t=a;
//    a=b;
//    b=t;
//}
//交换两个string类型
//void mySwap(string& a,string &b){
//    string t=a;
//    a=b;
//    b=t;
//}
template <typename T>
void mySwap(T& a,T &b){
    T t=a;
    a=b;
    b=t;
}
int main()
{
    int a=10,b=20;
    mySwap(a,b); //模板会根据传入实参类型,自动类型推导。T变成对应的类型
    cout<<a<<" "<<b<<endl;

    string s1="hello";
    string s2="world";
    mySwap(s1,s2);
    cout<<s1<<" "<<s2<<endl;
}

函数模板使用注意事项:

  1. typename和class都可以作为模板参数的说明
  2.  新的函数模板,就需要重新加上模板的声明

#include 
using namespace std;
class Role{
private:
    string name;
public:
    Role(string name){
        this->name=name;
    }
    string getName(){
        return name;
    }

};
template <typename T>
void mySwap(T& a,T &b){
    T t=a;
    a=b;
    b=t;
}

template <class T>
void add(T a, T b){
    cout<<a+b<<endl;
}

int main()
{

    Role tang("唐僧");
    Role MonkeyKing("孙悟空");
    mySwap(tang,MonkeyKing);
    cout<<"表面上是唐僧"<<"实际上是:"<<tang.getName()<<endl;

    int a=20,b=30;
    add(a,b);        //自动类型推导
    //add(a,b);  显示指明类型
    char ch='a',ch2='b';
    add(ch,ch2);
}

类模板

注意:类模板不支持自动类型推导,创建对象指明实际的参数类型

typename通常用于函数模板  class用于类模板

#include 
using namespace std;

template <class T,class T2>
class Demo{
private:
    T value1;
    T2 value2;
public:
    Demo(T v1,T2 v2){
        value1=v1;
        value2=v2;
    }
    void show(){
        cout<<value1<<" "<<value2<<endl;
    }
};
int main()
{
    Demo  student1("小明",20); 
    student1.show();

    Demo  rectangle(100,20);
    rectangle.show();

}

类模板声明和定义分离

#include 
using namespace std;

template <class T,class T2>
class Demo{
private:
    T value1;
    T2 value2;
public:
    Demo(T v1,T2 v2);
    void show();
};

template <class T,class T2>
Demo<T,T2>::Demo(T v1,T2 v2){
    value1=v1;
    value2=v2;

}

template <class T,class T2>
void Demo<T,T2>::show(){
    cout<<value1<<" "<<value2<<endl;
}
int main()
{
    Demo  student1("小明",20);
    student1.show();

    Demo  rectangle(100,20);
    rectangle.show();

}

2.string字符串 

string是C++内置的一个类,内部对char * 进行了封装实现。不用担心数组越界问题

构造函数

string(); //创建一个空的字符串 例如: string str;

string(const char* s); //使用字符串s初始化

string(const string& str); //使用一个string对象初始化另一个string对象

string(int n, char c); //使用n个字符c初始化

字符串追加

string& operator+=(const char* str); //重载+=操作符

string& operator+=(const char c); //重载+=操作符

string& operator+=(const string& str); //重载+=操作符

string& append(const char *s); //把字符串s连接到当前字符串结尾

string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾

string& append(const string &s); //同operator+=(const string& str)

string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾

char& operator[](int n); //通过[]方式取字符

char& at(int n); //通过at方法获取字符

插入

string& insert(int pos, const char* s); //插入字符串

string& insert(int pos, const string& str); //插入字符串

删除

string& erase(int pos, int n = npos); //删除从Pos开始的n个字符

截取字串

string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串。注意接收

替换

string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str

string& replace(int pos, int n,const char* s); //替换从pos开始的n个字符为字符串

#include 
using namespace std;
int main()
{
    string s;
    //empty()方法判断字符串内容是否为空  空的时候返回1  否则返回0
    cout<<s.empty()<<endl; //1
    string s2("Car");
    string s3(s2);
    cout<<s2<<" "<<s3<<endl;//Car Car
    cout<<s2.empty()<<endl; //0
    //增
    s2.append(" is run");
    cout<<s2<<endl;   //Car is run
    s2+=" on the way";
    cout<<s2<<endl;  //Car is run on the way

    //删
    //第一个参数 删除的起始位置
    //第二个参数 删除的个数
    s2.erase(0,4);
    cout<<s2<<endl; //is run on the way
    //改
    s2[0]='I';
    cout<<s2<<endl; //Is run on the way
    s2.at(0)='i';   //is run on the way
    cout<<s2<<endl;

    //替换
    //第一个参数:替换下标的起始位置
    //第二个参数:替换的个数
    //第三个参数:提出后的字符
    s2.replace(0,2,"are");
    cout<<s2<<endl; //are run on the way

    //截取字串 返回值需要string来接受
    string newStr=s2.substr(0,2);
    cout<<newStr<<endl; //ar
    cout<<s2<<endl; //are run on the way
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值