首页 > 其他分享 >Veriadic templates

Veriadic templates

时间:2024-04-13 19:14:09浏览次数:12  
标签:templates __ Veriadic ite auto args vec print

Veriadic templates

数量不定的模板参数

声明方式:

#pragma once
#ifndef __VARIADICT__
#define __VARIADICT__

/*
为什么需要定义这个空函数,因为传参到最后传入最后一位参数时后面的一组参数已经没有了.就是0
所以这个版本就是没有参数的.当最后一个变成0个的时候调用的是空方法
*/
void print() { }

template<typename T, typename... Types>
void print(const T& firstArg, const Types&... args) {
std::cout << firstArg << std::endl;
print(args...); // 注意这里的输出 -> 7.5是第一个参数firstArg
}

#endif // !__VARIADICT__

告诉编译器.该函数接受模板参数 -> 两组

  • 第一组是T

  • 第二组是args -> 个数不确定.只有在传入的时候才知道具体有几个

调用代码:

#include <iostream>
#include "variadic.h"

using namespace std;

int main() {
// 调用print
print(7.5, "hello", 42); // 7.5是第一个参数firstArg

return 0;
}

源代码里面的print(args...),实际上会把args...在传给print函数.进行递归打印

要知道后面的args具体有几个可以用方法sizeof...()

关键字auto

这是一个语法糖,由编译器去自动推断返回值类型

/*
在没有关键字auto之前声明类型
*/
list<string> c

list<string>::iterator ite;
ite = find(c.begin(), c.end(), target);

有了auto关键字以后

list<string> c;
auto ite = find(c.begin(), c.end(), target); // 他就回去自动推断find函数的返回类型
// auto不能拿来声明变量类型,auto在使用的同时变量必须同时赋值 -> 以下就是错误的
list<string> c;
auto ite;
ite = find(c.begin(), c.end(), target);

ranged-base for

新的for循环模式:

声明形式:

for (decl : coll) {
   statement;
}

for的左边必须声明一个变量.右边coll必须是一个容器

编译器会从右边的容器coll找出每一个元素.把每一个元素设置到左边的变量上.然后去做下面的内容

这个写法就不需要像之前的写法一样.找到容器的长度(类似forEach)

示例代码:

vector<double> vec;

for (auto elem : vec)
{
cout << elem*3 << endl; // 传递的是值给到elem
}

如果elem每一个*3,不会影响到vec里面的内容.因为传递的是值

示例代码2:

vector<double> vec;


for (auto& elem : vec)
{
elem *= 3; // 这里就会影响到vec里面的内容,因为传递的是引用 -> 指针地址
}

这样的话一个一个传出来是传递引用.所以elem * 3会影响vec里面的值

标签:templates,__,Veriadic,ite,auto,args,vec,print
From: https://www.cnblogs.com/JunkingBoy/p/18133208

相关文章

  • IDEA中Live Templates和Postfix Completion的用法
     前言 IDEA中代码生成的方式有两种LiveTemplatesPostfixCompletion这两种方式中,第一种基本每一个IDE都支持,但是支持第二中的很少。LiveTemplates输出模板1、sout,最基本的输出语句,快速生成System.out.println();2、soutp,快速生成参数输出语句。3、soutm,快速生成......
  • C++ templates: (1)、类模板
    1、类模板定义(主模板)template<typenameT,typenameC=list<T>,intMAX=10>classStack{public:usingvalue_type=T;public:Stack(constT&a):m_oContainer{move(a)}{cout<<"Stack<T,list<T>>()"<<......
  • C++ Function Templates (函数模板)
    C++FunctionTemplates[函数模板]1.TemplatesandGenericProgramming(模板与泛型编程)2.DefiningaFunctionTemplates(定义函数模板)2.1.InstantiatingaFunctionTemplate(实例化函数模板)2.2.TemplateTypeParameters(模板类型参数)2.3.Non......
  • cpp templates :auto推导
    目录常见推导函数的返回值常见推导1auto:产生拷贝,可以修改2auto&:左值引用,接受左值,可以修改3constauto&:const引用,可以接受左右值,不可修改4auto&&:万能引用,可以接受左右值,const引用时不能修改inta=100;constintb=100;autoa1=3;//a1为intautoa2......
  • Templates
    超级IO#defineOPENIOBUFnamespaceFASTIO{classFastIOBase{protected: #ifdefOPENIOBUFstaticconstintBUFSIZE=1<<16;charbuf[BUFSIZE+1];intbuf_p=0; #endifFILE*target;FastIOBase(FILE*f):targe......
  • 前端docx-templates生成word文档
    说明docx-templates项目地址:https://github.com/guigrpa/docx-templates原文:https://juejin.cn/post/7170695319004315679?searchId=202312171247306E0B93A485DAE6B4E304这个库能干啥?这个库能做的:替换Word模板中的文字实现FOR和IF操作在文档指定位置插入图片在模板里写......
  • C++ Templates 第2版 电子书 pdf
    关注公众号:红宸笑。回复:电子书即可  本书是同名经典畅销图书的全新升级版本,针对C++11、C++14、C++17标准进行了全面更新,并对所有的新语言特性(包括可变参数模板、通用lambda、类模板参数推导等)进行了解释。全书共28章。首先全面介绍了本书的内容结构和相关情况。第1部分(......
  • sap.fe.templates.ListReport.ExtensionAPI 的使用场合介绍
    SAPFioriElements是一种用于快速开发SAPFiori应用程序的框架,它通过提供预定义的UI元素和模板,简化了开发过程。其中,sap.fe.templates.ListReport.ExtensionAPI是SAPFioriElements框架中的一个重要组件,用于扩展ListReport应用程序的功能。SAPFioriElements概述在深入探讨s......
  • ElasticSearch之cat component templates API
    命令样例如下:curl-XGET"https://localhost:9200/_cat/component_templates?v=true&pretty"--cacert$ES_HOME/config/certs/http_ca.crt-u"elastic:ohCxPH=QBE+s5=*lo7F9"执行结果输出如下:nameversionalias_countm......
  • 无涯教程-D语言 - 模板(Templates)
    模板是通用编程的基础,它涉及以独立于任何特定类型的方式编写代码。函数模板将函数定义为模板会将其使用的一种或多种类型保留为未指定状态,以便稍后由编译器推导。在模板参数列表中定义了未指定的类型,该参数介于函数名称和函数参数列表之间。因此,函数模板具有两个参数列表-模板......