当前位置: 首页 > news >正文

洮南网站企业网站优化排名

洮南网站,企业网站优化排名,上海专业网站建设咨询,什邡门户网站欢迎来到我的Blog,点击关注哦💕 前言: C的模板在是泛型编程的重要组成部分,编写在不同类型上工作的代码,而无需为每个类型编写重复的代码,这有助于减少代码冗余并提高代码的可维护性。 模板 模板的介绍 …

欢迎来到我的Blog,点击关注哦💕

前言:

C++的模板在是泛型编程的重要组成部分,编写在不同类型上工作的代码,而无需为每个类型编写重复的代码,这有助于减少代码冗余并提高代码的可维护性。

模板

模板的介绍

  • C++模板是一种强大的编程技术,它允许程序员编写与类型无关的代码,即泛型编程。
  • 模板可以用于定义泛型函数和类,这些函数和类可以在编译时根据实际使用的数据类型进行实例化。
  • 模板的使用提高了代码的复用性和灵活性,减少了冗余代码的编写

模板分类

函数模板

函数模板的定义通常包含以下几个部分:

  1. template关键字:表示接下来的声明是一个模板。
  2. 模板参数列表:位于尖括号<>中,可以包含类型参数和非类型参数。
  3. 函数声明和定义:与普通函数类似,但使用模板参数列表中的类型来定义函数。
#include <iostream>
//函数模板
template <typename T>
T add(T a, T b) {return a + b;
}int main() {int x = 10, y = 20;double d1 = 1.5, d2 = 2.5;std::cout << "Adding two integers: " << add(x, y) << std::endl;std::cout << "Adding two doubles: " << add(d1, d2) << std::endl;return 0;
}

类模板

类模板的定义与函数模板类似,但用于定义类结构。以下是一个简单的类模板示例:

#include <iostream>template <typename T>
class SimpleContainer {
private:T data;public:SimpleContainer(T value) : data(value) {}T getData() const { return data; }void setData(T value) { data = value; }
};int main() {SimpleContainer<int> intContainer(10);std::cout << "Integer data: " << intContainer.getData() << std::endl;SimpleContainer<double> doubleContainer(3.14);std::cout << "Double data: " << doubleContainer.getData() << std::endl;return 0;
}

在这个例子中,SimpleContainer是一个类模板,它可以用于任何数据类型T。当在main函数中创建SimpleContainer的实例时,编译器会根据提供的类型参数自动生成相应的类实例。

类模板的成员函数

类模板的成员函数可以是模板函数,也可以是普通函数。模板成员函数在类模板的定义中直接声明,而普通成员函数可以在类模板外定义,此时需要显式指定模板参数。

template <typename T>
class MyClass {
public:void templateFunction(T value);void normalFunction();
};template <typename T>
void MyClass<T>::templateFunction(T value) {// Template member function
}template <typename T>
void MyClass<T>::normalFunction() {// Non-template member function, defined outside the class
}

函数模板实例化

函数模板的实例化通常发生在函数被调用时。编译器会根据传递给函数模板的参数类型,自动实例化一个特定类型的函数。例如:

template <typename T>
void print(T value) {std::cout << value << std::endl;
}int main() {print(10);    // 实例化为 void print(int)print(3.14);  // 实例化为 void print(double)
}

在这个例子中,print函数模板被两次调用,分别传入了intdouble类型的参数,因此编译器会为每种类型生成一个具体的函数实例。

类模板实例化

类模板的实例化发生在创建类模板的实例时,或者当类模板的成员函数被调用时。例如:

template <typename T>
class Box {
public:T value;Box(T v) : value(v) {}
};int main() {Box<int> intBox(10);   // 实例化为 class Box<int>Box<double> dblBox(3.14); // 实例化为 class Box<double>
}

这里,Box类模板被用于创建两个不同类型的实例,Box<int>Box<double>

隐式实例化与显式实例化

  • 隐式实例化:当模板被调用或使用时,编译器自动进行实例化。这是最常见的情况,如上述例子所示。
  • 显式实例化:在某些情况下,可能需要在编译器之外显式地实例化模板。这通常用于控制模板实例的生成,避免不必要的实例化,或者在编译时提前生成一些模板实例以提高运行时性能。显式实例化的语法如下:
template class Box<int>; // 显式实例化 Box<int>

非类型模板参数

  • 非类型模板参数是C++模板中的一种机制,它允许模板在编译时期使用常量值作为参数。
template<class T ,size_t N = 10>
class sulotion
{
public:sulotion(const T& x)	{_a[1] =  10;}void print(){for (int i = 0; i < 10; i++){cout << _a[i]<<" ";}cout << endl;}
private:T _a[N];
};

模板的特化

C++模板特化是一种技术,允许开发者为特定类型或类型模式提供不同的模板实现,以覆盖通用模板的默认行为。

  1. 必须要先有一个基础的函数模板
  2. 关键字template后面接一对空的尖括号<>
  3. 函数名后跟一对尖括号,尖括号中指定需要特化的类型
  4. 函数形参表: 必须要和模板函数的基础参数类型完全相同,如果不同编译器可能会报一些奇怪的错误。

全特化

  • 全特化是指为某个特定的模板参数提供专门的实现,通常用于处理某个特定类型的特殊情况。
  • 在全特化中,模板参数列表中的所有参数都被确定化,形成一个完全具体化的模板版本。
template<class T1, class T2>
class sulotion
{
public:sulotion(const T1& x, const T2& y): _data1(x),_data2(y){}void print(){cout << _data1 << " " << _data2 << endl;}
private:T1 _data1 ;T2 _data2  ;
};
//特化
template<>
class sulotion<int,char>
{
public:sulotion(const int& x, const char& y): _data1(x), _data2(y){}void print(){cout << _data1 << " " << _data2 << endl;}
private:int _data1;char _data2;
};

偏特化

  • 偏特化是指为一组模板参数提供专门的实现,而不是针对单个特定的参数。
  • 偏特化允许更细粒度的控制和更广泛的特化。类模板可以进行偏特化,但函数模板不支持偏特化,因为编译器无法区分不同的偏特化版本。
  • 在偏特化中,只有部分模板参数被确定化,而其他参数保持泛型。
template<class T1, class T2>
class sulotion
{
public:sulotion(const T1& x, const T2& y): _data1(x),_data2(y){}void print(){cout << _data1 << " " << _data2 << endl;}
private:T1 _data1 ;T2 _data2  ;
};
//偏特化
template<class T1>
class sulotion<T1,char>
{
public:sulotion(const T1& x, const char& y): _data1(x), _data2(y){}void print(){cout << _data1 << " " << _data2 << endl;}
private:T1 _data1;char _data2;
};

模板的分离编译

  • C++模板的分离编译是指将模板的声明和定义分离开来,以适应大型项目中模块化的开发需求。
  • 传统上,类模板的声明和定义通常放在同一个头文件中,但这种做法可能导致编译速度慢和编译依赖管理复杂。
  • 分离编译允许开发者将模板的声明放在头文件中,而将定义放在源文件中,这样可以减少头文件的大小,加速编译过程,并减少不必要的编译依赖。

模板分离的编译容易链接错误

解决方法

  • 将声明和定义放在同一个头文件中:这是最直接的方法,可以确保模板的声明和定义在编译时可见,从而避免链接错误。
  • 显示实例化:通过在源文件中显式实例化模板,告诉编译器为特定类型生成模板实例。这种方法虽然可以解决链接错误,但会降低代码的泛化能力,因为每次使用新类型时都需要更新实例化代码。

C++对模板的应用

  • 容器(Containers):提供了多种数据结构的实现,如 vectorlistdeque 等,这些容器通过模板类来支持不同数据类型的存储和管理。
  • 迭代器(Iterators):定义了一组接口,用于遍历容器中的元素。迭代器本身也是模板类,它们抽象了对容器元素的访问方式,使得算法能够独立于具体的数据结构实现。
  • 算法(Algorithms):提供了一系列的函数模板,用于执行常见的数据操作,如排序、搜索、复制等。这些算法通过模板定义,可以广泛应用于不同类型的容器。
  • 函数对象(Functors):也称为仿函数,是重载了 operator() 的对象,可以作为算法的参数,提供自定义的操作逻辑。函数对象同样是基于模板实现的,以支持不同类型的操作。
  • 适配器(Adapters):用于修改或包装现有容器或迭代器的接口,以满足特定的使用需求。适配器也是基于模板技术实现的,提供了灵活的扩展机制

C++反向迭代器

  • C++的反向迭代器是对正向迭代器的再次封装。

正向迭代器

//迭代器
template<class T,class Ref,class ptr>struct _list_iterator{typedef _list_node<T> Node;typedef _list_iterator<T, Ref,ptr> self;Node* _node;_list_iterator (Node* node):_node(node){}//重载operator*Ref& operator* (){return _node->_val;}//重载operator->ptr operator->(){return _node->_val;}//重载operator++(前置)self& operator++(){_node = _node->_next;return *this;}//重载operator++(后置)self& operator++(int){self tmp(*this);_node = _node->_next;return tmp;}//重载operator--(前置)self& operator--(){_node = _node->_prev;return *this;}//重载operator--(后置)self& operator--(int){self tmp(*this);_node = _node->_prev;return tmp;}//重载operator!=bool operator!=(const self& it)const {return _node != it._node;}//重载operator==bool operator==(const self& it)const{return _node == it._node;}};

反向迭代器

  • iteratorreverse_itrator是成为镜像对称

  • operartor*是解引用前一个数据,也就是迭代器的end

  • ++就是调用正向的–,反正 – 就是++

template<class iterator ,class Ref,class Ptr>struct Reverse_Iterator{typedef Reverse_Iterator<iterator, Ref, Ptr> self;iterator _it;Reverse_Iterator(iterator it):_it(it){}Ref operator* (){iterator tmp(_it);return *(--tmp);}Ptr operator->(){return &(operator* ());}self& operator++(){--_it;return *this;}self& operator--(){++_it;return *this;}bool operator==(const self& s)const{return _it == s._it;}bool operator!=(const self& s)const{return _it != s._it;}};

文章转载自:
http://rheims.nLcw.cn
http://acrocephalia.nLcw.cn
http://unrestraint.nLcw.cn
http://fishhook.nLcw.cn
http://carriageable.nLcw.cn
http://managua.nLcw.cn
http://cataphract.nLcw.cn
http://fledge.nLcw.cn
http://xxxv.nLcw.cn
http://senarmontite.nLcw.cn
http://saintpaulia.nLcw.cn
http://shepherd.nLcw.cn
http://bystreet.nLcw.cn
http://skim.nLcw.cn
http://whizbang.nLcw.cn
http://polycistronic.nLcw.cn
http://demagogy.nLcw.cn
http://whipworm.nLcw.cn
http://sudanese.nLcw.cn
http://skiamachy.nLcw.cn
http://hyperfunction.nLcw.cn
http://quilled.nLcw.cn
http://dpi.nLcw.cn
http://congregant.nLcw.cn
http://amplification.nLcw.cn
http://dawn.nLcw.cn
http://disagree.nLcw.cn
http://recomputation.nLcw.cn
http://consenting.nLcw.cn
http://abashment.nLcw.cn
http://knurled.nLcw.cn
http://yod.nLcw.cn
http://smolt.nLcw.cn
http://oppositely.nLcw.cn
http://subsultive.nLcw.cn
http://cataphonic.nLcw.cn
http://mulish.nLcw.cn
http://promin.nLcw.cn
http://germen.nLcw.cn
http://abolitionist.nLcw.cn
http://microfaction.nLcw.cn
http://dishevel.nLcw.cn
http://lightkeeper.nLcw.cn
http://maidstone.nLcw.cn
http://skycap.nLcw.cn
http://pechora.nLcw.cn
http://enology.nLcw.cn
http://das.nLcw.cn
http://trident.nLcw.cn
http://intermissive.nLcw.cn
http://unbowed.nLcw.cn
http://paddywhack.nLcw.cn
http://isocheim.nLcw.cn
http://sidewise.nLcw.cn
http://metathorax.nLcw.cn
http://exactor.nLcw.cn
http://renierite.nLcw.cn
http://pedagoguism.nLcw.cn
http://sequencer.nLcw.cn
http://thou.nLcw.cn
http://decretive.nLcw.cn
http://scapulary.nLcw.cn
http://suavity.nLcw.cn
http://hominy.nLcw.cn
http://sweater.nLcw.cn
http://vandal.nLcw.cn
http://autocoherer.nLcw.cn
http://patsy.nLcw.cn
http://pram.nLcw.cn
http://traxcavator.nLcw.cn
http://knoxville.nLcw.cn
http://hydrosere.nLcw.cn
http://accentor.nLcw.cn
http://glomma.nLcw.cn
http://versatilely.nLcw.cn
http://naevi.nLcw.cn
http://hydroperoxide.nLcw.cn
http://emergence.nLcw.cn
http://sixtyfold.nLcw.cn
http://poncho.nLcw.cn
http://bookman.nLcw.cn
http://escapee.nLcw.cn
http://trilobed.nLcw.cn
http://neurochemist.nLcw.cn
http://scatback.nLcw.cn
http://joltheaded.nLcw.cn
http://puppydom.nLcw.cn
http://amort.nLcw.cn
http://deathsman.nLcw.cn
http://marvin.nLcw.cn
http://perugia.nLcw.cn
http://actionist.nLcw.cn
http://reheat.nLcw.cn
http://outlawry.nLcw.cn
http://cutline.nLcw.cn
http://chewy.nLcw.cn
http://amphicoelian.nLcw.cn
http://themselves.nLcw.cn
http://avoset.nLcw.cn
http://anchoretic.nLcw.cn
http://www.15wanjia.com/news/101045.html

相关文章:

  • 开平网站建设公司怎么优化网站排名才能起来
  • 网站建设的网络技术搜索引擎营销的四种方式
  • 单页网站制作系统杭州seo价格
  • 网站客服系统源码深圳网站制作哪家好
  • 如何使用ps做网站web免费网站
  • 从零开始做电影网站班级优化大师是干什么用的
  • 可以做围棋习题的网站推广系统
  • 优度公司做的网站邯郸网站建设优化
  • 坪山网站建设服务广州网站关键词推广
  • 营销网站建设方案互联网推广广告
  • 沈阳学习做网站搜索推广是什么意思
  • 迁安网站建设二级域名分发平台
  • 自己做团购网站怎么样网站建设策划书案例
  • 深圳比较好的ui设计公司seo推广如何做
  • 网站备案后要做什么推广赚钱的软件排行
  • 怎么黑人网站企业培训师
  • 怎么做asp网站百度霸屏推广多少钱一个月
  • 企业网站如何上存抖音seo关键词排名技术
  • 做网站花的钱和优化网站有关系吗seo网课培训
  • 做电影下载网站需要什么软件好深圳博惠seo
  • WordPress社群seowhy培训
  • 做包装找灵感看什么网站软文营销模板
  • 有没有教做帽子的网站b站推广渠道
  • 一个新手怎么做推广关键词优化推广公司排名
  • icp备案网站更名ios aso优化工具
  • 京东旗下的企业网站有哪些百度软件开放平台
  • 网站设计标准字体2020年可用好用的搜索引擎
  • 口碑好的聊城网站建设提高百度快速排名
  • 网站建设实训意见和建议软文营销广告案例
  • 天津艺匠做网站软文例文 经典软文范例