栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++面向对象高级

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++面向对象高级

函数模板
  1. 模板的定义和模板的使用

    #include "iostream"
    using namespace std;
    template 
    void myswap(T& a,T& b){
        T swap;
        swap=a;
        a=b;
        b=swap;
    }
    int main(){
        int a =10;
        int b=90;
        myswap(a,b);
        myswap(a,b);
        cout< 
  2. 函数模板只有是显式指定类型的时候才会发生自动类型转换(也建议使用模板的时候指定类型)

  3. 函数模板和普通函数的优先级后者更高,可以使用空模板参数来强制调用函数模板

  4. 函数模板可以产生重载

  5. 模板的具体化(使用重载)可以解决T为特殊类型的问题

    template 
    bool comp(T& a ,T& b ){
        if(a==b){
            return true;
        } else{
            return false;
        }
    }
    
    template<> bool comp(Person& a ,Person& b){
        if(a.age==b.age && a.height==b.height){
            return true;
        } else{
            return false;
        }
    
    }
    
类模板
  1. 类模板的定义
template
class Person{
public:
    Person(NameType name , AgeType age){
        this->age=age;
        this->name=name;
    }
    NameType name ;
    AgeType age;
};

int main(){
    Personp1("kafen",21);
    cout< 
  1. 类模板只能显式指定类型,类模板在参数列表中可以有默认参数,下面的不是自动类型推导,而是指定了类型
template
class Person{
public:
    Person(NameType name , AgeType age){
        this->age=age;
        this->name=name;
    }
    NameType name ;
    AgeType age;
};

int main(){
    Person<>p1("kafen",21);
    cout< 
  1. 类模板中的成员函数的创建时机:在调用的时候才去创建

  2. 类模板对象作为函数参数,使用广泛的是指定类型,比如void printPerson1(Person &p)

  3. 类模板和继承:父类是一个类模板时,子类继承时需要指定父类中T的类型,如果想灵活指定,子类也需要变成一个类模板

    template 
    class Base{
    public:
        T t;
    };
    class Son1: public Base{//指定类型
    public:
        string b ;
    };
    
    template
    class Son2:public Base{//将子类变成类模板
    //
    };
    
  4. 类模板成员函数类外实现

    template 
    class Base{
    public:
        T t;
        Base(T t);
    };
    
    template 
    Base::Base(T t) {
        cout<
        Base(3);
        return 0;
    }
    
  5. 分文件编写的类模板:将声明和实现都写到hpp文件中

  6. 类模板和友元:建议全局函数类内实现

    template 
    class Base{
        friend void ShowArgs(Base base){//全局函数类内实现
            cout<//构造函数
            this->t=t;
        }
    };
    
    int main(){
        Base b1(10);
        ShowArgs(b1);
        return 0;
    }
    
类模板的案例:实现一个数组类 标准模板库(STL)
  1. 容器:序列式容器,关联性数据
  2. 算法:质变算法(改元素)和非质变算法(不改元素)
  3. 迭代器:连接算法和容器,暂时可以先认为迭代器是一个指针
string容器
  1. string的构造没有可比性,我使用string s = “str”
  2. string的赋值使用=就可
  3. 字符串拼接相当于是拼接,使用+=就可
  4. 字符串查找find(),字符串替换replace(int,int,string*)
  5. 字符串比较compare()
  6. 字符串插入insert()和删除erase()
  7. 获取子串substr(int pos , int n)
Vector容器
  1. 遍历数组:可以只使用迭代器,也可以使用算法
#include "iostream"
#include "vector"
#include "algorithm"
using namespace std;

void func(int i ){
    cout<
    vector v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    vector::iterator pbegin =v.begin();
    vector::iterator pend =v.end();
    while(pbegin!=pend){
        cout<<*pbegin<::iterator t=v.begin();t!=v.end();t++){
        cout<<*t< 
  1. vector容器中放置自定义数据类型,也可以存放指针,只需要把vector::iterator中的int替换成对应的数据类型或者指针。
  2. 容器的嵌套(类似指针)
int main(){
    vector v01;
    vector v02;

    vector> v0;
    v01.push_back(1);
    v01.push_back(2);
    v01.push_back(3);

    v02.push_back(10);
    v02.push_back(20);
    v02.push_back(30);

    v0.push_back(v01);
    v0.push_back(v02);

    for(vector>::iterator it_outside = v0.begin();it_outside!=v0.end();it_outside++){
        for(vector::iterator it_inside=(*it_outside).begin();it_inside!=(*it_outside).end();it_inside++){
            cout<<*it_inside< 
deque 容器 
stack 容器 
queue容器 
list容器(linklist) 
set/multiset容器 
pair容器 
map/multimap容器 
函数对象 
谓词 
内建函数 
算法
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/853979.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号