在C++中判断模版实例化后的数据类型

C++之模板 泛型编程 泛型编程即以一种独立于任何特定类型的方式编写代码。 模板是创建泛型类或函数的蓝图或公式。 函数模板 函数模板定义的一般形式 template <typename type> ret-type func-name(parameter list) { // 函数的主体 } type 是函数所使用的数据类型的占位符名称。这个名称可以在函数定义中使用。 具体用法见下面案例 #include <iostream> #include <string> using 阅读详情

问题:

在自定义的模版类中,根据实例化后的数据类型来进行不同的打印输出

// class templates
#include <iostream>
#include <typeinfo.h>
using namespace std;

template <class T>
class mypair {
	T a, b;
	T * pTable;
	int TableLength;
public:
	mypair (T first, T second)
	{a=first; b=second;}
	mypair (void* pIn);
	mypair (void* pIn,int n);
	T getmax ();
	void printTable();
};

template <class T>
T mypair<T>::getmax ()
{
	T retval;
	retval = a>b? a : b;
	return retval;
}

template <class T>
mypair<T>::mypair (void* pIn)
{
	pTable = (T*)pIn;
}
template <class T>
mypair<T>::mypair (void* pIn, int n)
{
	pTable = (T*)pIn;
	TableLength = n;
}
template <class T>
void mypair<T>::printTable()
{
	//int tmp;
	//string mystring(&typeid(tmp).name);
	printf("%s \n",typeid(T).name());
	//const char*pchar = typeid(tmp).name();
	if (!strcmp(typeid(T).name(),"int"))
	{
		for (int i=0;i<TableLength;i++)
		{
			printf("%d \t",pTable[i]);
		}
		printf("\n");
	}
	else if (!strcmp(typeid(T).name(),"float"))
	{
		for (int i=0;i<TableLength;i++)
		{
			printf("%f \t",pTable[i]);
		}
		printf("\n");
	}
}
int main () {
	mypair <int> myobject (100, 75);

	cout << myobject.getmax()<<endl;


	int inDate [11] ={0,1,2,3,4,5,6,7,8,9,10};
	mypair<int> myobject2((void*)inDate,6);
	myobject2.printTable();

	float inDateFloat [11] ={0.0,1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.0};
	mypair<float> myobject3((void*)inDateFloat,6);
	myobject3.printTable();
	return 0;
}

输出:

100
int
0       1       2       3       4       5
float
0.000000        1.100000        2.200000        3.300000        4.400000        5.500000
请按任意键继续. . .


头文件:#include <typeinfo>

http://www.cplusplus.com/reference/typeinfo/type_info/

http://en.cppreference.com/w/cpp/language/typeid

http://en.cppreference.com/w/cpp/types/type_info

FPGA时序约束--实战篇(读懂Vivado时序报告) 本文将详细介绍如何读懂Vivado时序报告,包括报告的基本结构和如何分析报告。 阅读详情

相关推荐

车载与安防项目实战:如何用RK3588搭配NVP6158C实现4路AHD摄像头BT1120接入与同步预览

本文详细解析了基于RK3588处理器与NVP6158C解码芯片实现4路AHD摄像头BT1120接入与同步预览的技术方案。针对车载环视和安防监控场景,重点探讨了硬件架构设计、BT1120接口配置、多路同步机制及Android HAL适配等核心问题,提供了从底层驱动到上层应用的完整实现路径,为相关领域的工程实践提供可靠参考。

weixin_30571465的博客 496

C++中template如何判断类型

在C++中,使用template,有时候可能会需要得到当前所使用的类型以下为所解决的办法:TT类为使用模板的类,TT.h#ifndef _TT_H#define _TT_H#include "stdio.h"template class TT{public:    void printType();};templatevoid TT::printType(){    printf("int/n");}templatevoid TT::printType(){    printf("char/n");}tem

mykoma521的专栏 1万+

10.2 在新能源、储能及电网节点的应用实践

构网型变流器技术在电力系统中的应用进展 构网型变流器技术正从理论研究走向规模化工程应用,为新能源并网提供主动支撑能力。该技术在光伏电站采用"部分构网"混合架构,由构网单元实现黑启动和弱网稳定;在风电场利用叶轮动能提供惯量响应。储能系统是理想的构网载体,可提供快速调频和动态电压支撑。在电网节点,构网型STATCOM可增强弱电网电压稳定性,大容量变流器可替代传统调相机。当前面临标准滞后、宽频振荡风险等共性挑战,需在可靠性、经济性间取得平衡。系统级协调控制和精确阻抗建模是应用成功的关键。

技术工作者 1123

c++ 模板类型推断

1.若T为引用,但不是万能引用: template<class T> void fun(T&); a)T不会被推导为引用类型,形参会被推断为引用类型(&类型) b)如果实参带const,实参的const属性会成为模板T的组成部分,且形参也会带上const属性(const &) 2.若T为引用,且带有const属性 template<class T> void fun(const T&); a) T不会被推断为const类型(即使实参.

sbsbsbsbsb_1的博客 2033

webpack性能优化-optimization.splitChunks.chunks中的“all“、“async“和“initial“

最初,chunks(以及内部导入的模块)是通过内部 webpack 图谱中的父子关系关联的。CommonsChunkPlugin 曾被用来避免他们之间的重复依赖,但是不可能再做进一步的优化。 ——摘自 webpack 从 webpack v4 开始,移除了 CommonsChunkPlugin,取而代之的是 optimization.splitChunks。 而关于 optimization.splitChunks 中的chunks,webpack 官方文档却没有很好的进行举例解释,只说明了 async.

程序员秋风的博客 3138

41-dify案例分享-基于database插件实现Text2sql的数据库查询图表工作流

本文介绍基于 Text2SQL 的 Dify 工作流实现过程。先阐述 Text2SQL 概念、步骤及应用领域。接着说明工作流制作,包括用 chatflow 加开场白、获取系统时间,配置 LLM 生成 SQL,用 SQL Execute 工具查询,经 LLM 汇总结果,代码生成 ECharts 数据,条件分支判断图表类型输出。最后介绍验证测试、提供体验地址,还提及借助工具生成建表和初始化数据 SQL 语句。

个人技术 1万+

C++中判断数据的类型

C++中判断数据的类型 在实际的项目中经常要进行数据类型的转换,特别是字符串转成数字或者数字转字符串,而在进行数据类型转换之前要先知道数据的类型。 这时如何判断一个数据的数据类型这个问题就自然而然的遇到了。在本人几次遇到这个问题的情况下,这次来做一个总结。 1.typeid typeid这个函数在头文件typeinfo.h中,在使用这个函数时要加上头文件:#include<...

qq_38502736的博客 4万+

VirtualBox虚拟机上ubuntu系统安装问题记:显示画面太小、启动就异常退出等

报错:Failed to load R0 module D:/VMMR0.r0: The path is not clean of leading double slashes: ‘D:/VMMR0.r0’ (VERR_SUPLIB_PATH_NOT_CLEAN).解决方法特别简单,就是安装VIrtualBox的时候不要安装在CDE等盘根目录下。首先VirtualBox的下载安装过程是比较简单的,此处就不赘述了。然后E盘下新建个目录,安装到这个目录下就可解决。

qq_42595610的博客 2832

C++泛型编程

泛型实质上就是不使用具体的数据类型(如 int、double 等),而是使用一种通用的类型来进行程序设计的方法,将程序尽可能通用,将算法从数据结构中抽象出来,成为通用算法。

weixin_73249104的博客 443

直方图均衡化原理及matlab和opencv实现代码

详细分析了直方图均衡化的数学原理,配有matlab和opencv实现代码

qq_21603315的博客 4780

Android 12.0 系统systemui状态栏下拉左滑显示通知栏右滑显示控制中心模块的流程分析

在android12.0的系统rom定制化开发中,在系统原生systemui进行自定义下拉状态栏布局的定制的时候,需要在systemui下拉状态栏下滑的时候,根据下滑坐标来 判断当前是滑出通知栏还是滑出控制中心模块,所以就需要根据屏幕宽度,来区分x坐标值为多少是左滑出通知栏或者右滑出控制中心模块,接下来分析下 状态栏下滑坐标等相关功能代码分析

安卓兼职framework和app工程师的博客 2773

c++ 函数模版

如果需要多个将同一个算法用于不同类型的函数,请使用模版 函数模版:要让编译器知道程序需要一个特定形式的交换函数,只需在程序中使用Swap()函数即可,编译器将检查所使用的参数类型,并生成相应的函数, 1. template<typename AnyType> void Swap(AnyType &a, AnyType&b) { AnyType tem...

ren1204的博客 183

Openclaw接入大模型API,操作本地windows文件

本文介绍了在WSL2环境下配置OpenClaw使用阿里百炼API的方法。主要内容包括:1)确认WSL2安装完成;2)配置阿里百炼API(需注意Qwen3-Max-Thinking为收费模型);3)提供正确的JSON配置格式示例,强调apiKey必须填写真实值;4)说明配置错误需修改Linux配置文件;5)建议通过询问模型信息验证连接成功。后续将介绍如何切换免费API及本地文件访问功能。

qq_27246521的博客 994

浅谈C++中模版的使用

泛型编程(Generic Programming)是一种编程范式,通过将类型参数化来实现在同一份代码上操作多种数据类型,泛型是一般化并可重复使用的意思。泛型编程最初诞生于C++中,目的是为了实现C++的STL(标准模板库)。 模板(template)是泛型编程的基础,一个模板就是一个创建类或函数的蓝图或公式。例如,当使用一个vector这样的泛型类型或者find这样的泛型函数时,我们提供足够

lkn910907的专栏 820

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

《深度剖析:C++中实例化一个对象的必经阶段》

定义一个类 MyClass ,其中有一个 int 类型的成员变量 x 和一个自定义类 AnotherClass 类型的成员变量 y ,在对象创建时, x 会被初始化为 0 , y 会调用 AnotherClass 的默认构造函数进行初始化。在 C++中,如果一个类包含了虚函数,那么在实例化该类的对象时,会涉及到虚函数表和虚函数指针的初始化。例如,在一个大型项目中,定义的全局配置类的对象,其内存就是在编译阶段就分配好的,在程序运行的全过程中都可以使用该对象来获取配置信息。

xy520521的博客 826

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

C++基本数据类型

C++ 数据类型 使用编程语言进行编程时,需要用到各种变量来存储各种信息。变量保留的是它所存储的值的内存位置。这意味着,当您创建一个变量时,就会在内存中保留一些空间。 您可能需要存储各种数据类型(比如字符型、宽字符型、整型、浮点型、双浮点型、布尔型等)的信息,操作系统会根据变量的数据类型,来分配内存和决定在保留内存中存储什么。 基本的内置类型 C++ 为程序员提供了种类丰富的内置数据类型和用户自定...

CHMBK的博客 502

C# 与 C++ 数据类型对照

C++            C#=====================================WORD            ushortDWORD            uintUCHAR            int/byte   大部分情况都可以使用int代替,而如果需要严格对齐的话则应该用bytebyte UCHAR*            str

okadler0518的专栏 3464

c++ 模版详解

1. 模板的概念。 我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同。正确的调用重载函数。例如,为求两个数的最大值,我们定义MAX()函数需要对不同的数据类型分别定义不同重载(Overload)版本。 //函数1. int max(int x,int y); {return(x>y)?x:y ;} //函数2.

feiyangtianyao的专栏 719

C++第2章 数据类型

目录 2.1 第1个C++程序 2.2 常量及符号 2.3 变量 2.3.1 基本知识 1.常见数据类型取值范围: 2.标识符 3.关键字 2.3.2常见数据类型使用 1.整型int 2.实型float、double 3.字符型char 4.转义字符 5.字符串型string 6.布尔类型bool 2.4数据输入(<<)与输出(>>) 2.4.1.C++语言中的流: 标准I/O有4个类对象 cin(键盘)、cout(显示器)、cerr、clog...

Catherinemin的博客 1095

C++模板初阶

函数模板与类模板。

天行健、君子以自强不息 1258

C++ template学习二 类模板定义及实例化

一个类模板(也称为类属类或类生成类)允许用户为类定义一种模式,使得类中的某些数据成员、默写成员函数的参数、某些成员函数的返回值,能够取任意类型(包括系统预定义的和用户自定义的)。 如果一个类中数据成员的数据类型不能确定,或者是某个成员函数的参数或返回值的类型不能确定,就必须将此类声明为模板,它的存在不是代表一个具体的、实际的类,而是代表着一类类。 //templatedemo.h#i...

aofen2904的博客 254

几种特殊的数据类型的判断

举例: 说明:因为()的优先级在*前面,所以有无括号会造成不同的结果 1.int (*x)[4]指向数组的指针 数组指针 2.int *(x)[4]保存指针的数组 指针数组 3.int *f();返回值为指针的函数 指针函数 4.int (*f)();指向函数的指针 函数指针 例题分析: 一维数组: int arr[4]; 数据类型 arr: ...

Rain的博客 589

C与C++的数据类型

System Types for C and C++ 探索C/C++默认数据类型 char: 通常但不总是8bits, corresponding with the ASCII, 编译器区别对待它和一般整型。 #include <iostream> int main(void) { int i = 0x42; char c = 0x42; const char *str = "Hello World\n"; std::cout << i << '\n'; //

u013214951的博客 285

[C++学习日记]-03-基本数据类型的认识

数据类型是什么,他到底有什么作用呢 数据类型 在编程时,您可能需要存储各种数据类型的信息,操作系统会根据变量的数据类型,来分配内存和决定在保留内存中存储什么。 数据类型定义了标识符的正确使用,可以存储什么类型的数据,以及可以执行哪些类型的操作。 提示:C++ 为程序员提供了种类丰富的内置数据类型。 表达式 下面列出了一些合法和非法的C++ 表达式例子。 61+39 // 合法 // + 运...

Timeless小帅的博客 384

C++模板(初阶)

本章主要讲解了泛型编程、函数模板、函数模板概念、函数模板的格式、函数模板的原理、函数模板的实例化中的隐式实例化、显式实例化、模板参数的匹配原则、类模板等等

weixin_47257473的博客 800
上一篇: _ _int64 64位整数
下一篇: 模版类
cmsbupt
博客等级 码龄19年 67粉丝 86原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值