【cmu15445c++入门】(4)c++中的模板方法

这篇具有很好参考价值的文章主要介绍了【cmu15445c++入门】(4)c++中的模板方法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、template模板方法

模板方法是c++的一个特性,可以让你的代码在不指定数据类型的情况下,运行不同的数据类型。

你可以创建模板方法和模板类,本文讨论模板方法。

二、代码


// Includes std::cout (printing) for demo purposes.
#include <iostream>

// Templates are a language feature in C++ that allow you to write code that
// can work with multiple data types, without actually specifying those types.
// In C++, you can create both templated functions and templated classes. We'll
// talk about templated functions in this file.

//模板方法是c++的一个特性,可以让你的代码在不指定数据类型的情况下,运行不同的数据类型。
//你可以创建模板方法和模板类,这个文件讨论模板方法。

// Here is a basic templated function that adds two numbers.
// Syntax note: You will see code with both template<class T> and
// template<typename T>. Although these statements are equivalent, there are
// differences between the class and typename keywords. This blog article covers
// this difference, but you won't need to know this for the class:
// https://mariusbancila.ro/blog/2021/03/15/typename-or-class/

//模板方法,计算两个数字的和
template <typename T> 
T add(T a, T b) { return a + b; }

// It is possible to pass multiple type names via templates into functions.
// This function will print both of these values out.
// 传入多个类型的模板方法
template<typename T, typename U>
void print_two_values(T a, U b) {
  std::cout << a << " and " << b << std::endl;
}
template<typename T, typename U, typename V>
void print_three_values(T a, U b,V c) {
  std::cout << a << " and " << b << " and " << c <<std::endl;
}
template<typename T, typename U, typename V,typename W>
void print_four_values(T a, U b,V c,W d) {
  std::cout << a << " and " << b << " and " << d <<std::endl;
}
template<typename T, typename U, typename V,typename W,typename X>
void print_five_values(T a, U b,V c,W d,X e) {
  std::cout << a << " and " << b << " and " << e <<std::endl;
}

// It is also possible to create specialized templated functions, that do
// different things for different types. Take the following contrived example,
// which prints the type if its a float type, but just prints hello world for
// all other types.
// 也能根据不通的类型,制定不同的模板方法。
template <typename T> 
void print_msg() { std::cout << "Hello world!\n"; }

// Specialized templated function, specialized on the float type.
// 对float类型单独定制
template <> 
void print_msg<float>() {
  std::cout << "print_msg called with float type!\n";
}

// Lastly, template parameters do not have to be classes. Take this basic (yet
// very contrived) function that takes in a bool as a template parameter and
// does different things to the argument depending on the boolean argument.
// 这个做法不推荐
template <bool T> int add3(int a) {
  if (T) {
    return a + 3;
  }

  return a;
}

int main() {
  // First, let's see the add function called on both ints and floats.
  std::cout << "Printing add<int>(3, 5): " << add<int>(3, 5) << std::endl;
  std::cout << "Printing add<float>(2.8, 3.7): " << add<float>(2.8, 3.7)
            << std::endl;

  // It is also possible for a templated function to interpret the type of its
  // arguments, although if you're a beginner in modern C++, it's preferred you
  // don't do this because then you might not be sure of the types being passed
  // into your functions.
  // 也可以不指定类型,但初学者不建议
  std::cout << "Printing add(3, 5): " << add(3, 5) << std::endl;

  // Second, let's see the print_two_values function being called with two
  // different types.
  std::cout << "Printing print_two_values<int, float>(3, 3.2): ";
  print_two_values<int, float>(3, 3.2);

  print_three_values<int, float, double>(3, 3.2,2.14);
  print_four_values<int, float, double,float>(3, 3.2,2.14,2.12);
  print_five_values<int, float, double,float,int>(3, 3.2,2.14,2.12,6);
  // Let's see what happens when we called print_msg with and without the float
  // type being passed in. As expected, the first call to print_msg prints out
  // the general output, while the second one, with the float argument,
  // recognizes its type parameter and calls the specialized function.
  std::cout << "Calling print_msg<int>(): ";
  print_msg<int>();
  std::cout << "Calling print_msg<float>(): ";
  print_msg<float>();

  // add3 has the specified behavior for both a true and false templated
  // argument, as we can see here.
  std::cout << "Printing add3<true>(3): " << add3<true>(3) << std::endl;
  std::cout << "Printing add3<false>(3): " << add3<false>(3) << std::endl;

  // Lastly, it's important to note that most of these are contrived examples,
  // and it is possible to code some of these functions (e.g. passing a boolean
  // as an argument instead of a templated argument) without using templates.
  // However, in the class, you'll be seeing code similar to this in the
  // codebase, so it's good to understand templated functions in these contexts!
  // 最后,需要注意的是,以上大多数都是人为的示例,并且有的是可以不使用模板,而是对其中一些函数进行编码
  //(例如,将布尔值作为参数而不是模板化参数传递)。但是,海量的代码库中很可能也看到与此类似的代码,因此最好在这些上下文中理解模板化函数!
  return 0;
}

运行结果

【cmu15445c++入门】(4)c++中的模板方法,c++文章来源地址https://www.toymoban.com/news/detail-783676.html

到了这里,关于【cmu15445c++入门】(4)c++中的模板方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • CMU15445 (Fall 2020) 数据库系统 Project#2 - B+ Tree 详解(上篇)

    考虑到 B+ 树较为复杂,CMU15-445 将 B+ 树实验拆成了两部分,这篇博客将介绍 Checkpoint#1、Checkpoint#2 删除操作和迭代器的实现过程,搭配教材 《DataBase System Concepts》食用更佳。 许多查询只涉及文件中的少量记录,例如“找出物理系所有教师”的查询就只涉及教师记录中的一小部

    2024年02月08日
    浏览(33)
  • 模板方法中的线程安全问题

    是否存在临界区,共享的变量,会被不同线程写入 那么模板方法里面基类的成员变量或者方法就会存在线程安全问题   AbstractExcelSheet 业务数据和excel 逻辑 解耦 让data 可以 在service 层之间set进来 这样excel的相关类不用添加到 spring 容器中   BusiPackageResultExcelSheet   BusiPkgRuleRe

    2024年02月02日
    浏览(22)
  • 在dedecms模板中将面包屑导航中的"首页"换成小图标的方法

    dedecms织梦模板将面包屑导航中的首页换成图片的方法 我们打开 /include/typelink.class.php 文件 找到: 修改为:   yii666提醒您: 上面修改后,你的那个小图标要放到/style/目录,文件名为genban.jpg

    2024年02月03日
    浏览(40)
  • JAVA8-lambda表达式8:在设计模式-模板方法中的应用

    JAVA8-lambda表达式1:什么是lambda表达式 JAVA8-lambda表达式2:常用的集合类api JAVA8-lambda表达式3:并行流,提升效率的利器? JAVA8-lambda表达式4:Optional用法 java8-lambda表达式5:toMap引发的线上故障 JAVA8-lambda表达式6:重构和定制收集器 JAVA8-lambda表达式7:重要的函数接口 最近在公司

    2024年02月14日
    浏览(40)
  • JavaScript从入门到精通系列第二十八篇:详解JavaScript中的字符串的方法

      文章目录 前言 一:String中的方法 1:获取字符串的长度 2:返回指定位置的字符 3:返回指定位置的字符Unicode编码 4:返回指定位置的字符Unicode编码  二:比较常用的 1:连接两个字符串 2:检索一个字符串中指定内容  3:从后检索一个字符串中指定内容   4:截取字符串

    2024年02月06日
    浏览(44)
  • 【C++从入门到放弃】模板介绍(函数模板、类模板)

    🧑‍💻作者: @情话0.0 📝专栏:《C++从入门到放弃》 👦个人简介:一名双非编程菜鸟,在这里分享自己的编程学习笔记,欢迎大家的指正与点赞,谢谢!   以我们之前所学的知识,假如要实现一个通用的加法函数,那么可以通过函数重载的方式来实现。 使用函数重载虽

    2023年04月14日
    浏览(31)
  • JavaScript从入门到精通系列第三十一篇:详解JavaScript中的字符串和正则表达式相关的方法

      文章目录 知识回顾 1:概念回顾 2:正则表达式字面量 一:字符串中正则表达式方法 1:split 2:search 3:match 4:replace         正则表达式用于定义一些字符串的规则,计算机可以根据正则表达式检查一个字符串是否符合规则,或者将字符串中符合规则的内容提取出来。

    2024年01月17日
    浏览(47)
  • CMU 15-445 -- Logging Schemes - 17

    本系列为 CMU 15-445 Fall 2022 Database Systems 数据库系统 [卡内基梅隆] 课程重点知识点摘录,附加个人拙见,同样借助CMU 15-445课程内容来完成MIT 6.830 lab内容。 数据库在运行时可能遭遇各种故障,这时可能同时有许多正在运行的事务,如果这些事务执行到一半时故障发生了,就可能

    2024年02月15日
    浏览(25)
  • OpenCV中的模板匹配

    OpenCV中的模板匹配 模板匹配是一项常见的计算机视觉任务,其目的是从输入图像中找到与给定模板最相似的部分。在OpenCV中,我们可以使用模板匹配算法来识别某个图案或对象在另一个图像中的位置。本文将介绍如何使用OpenCV进行模板匹配,并提供相应的源代码。 1.读取图像

    2024年02月06日
    浏览(34)
  • c++中的类模板

    C++的类模板为生成通用的类声明提供了一种更好的方法。模板提供参数化类型,即能够将类型名作为参数传递给接收方来建立类或者函数。 不能将模板成员函数放在独立的实现文件中(以前,C++提供了export,让您能够将模板成员函数放在独立的实现文件中,但是支持的

    2024年02月14日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包