溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用c中變長(zhǎng)參數(shù)

發(fā)布時(shí)間:2021-10-14 13:53:55 來(lái)源:億速云 閱讀:143 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容主要講解“如何使用c中變長(zhǎng)參數(shù)”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何使用c中變長(zhǎng)參數(shù)”吧!

1.使用模板中的變長(zhǎng)參數(shù)函數(shù)聲明

#include <iostream>
using namespace std;

/*變長(zhǎng)參數(shù)函數(shù)模板聲明*/
template <typename... T>
void print(T... val);

/*邊界條件*/
void print(void)
{
cout<<"here end"<<endl;
}

/*遞歸的特例化定義*/
template <typename T1, typename... T2>
void print(T1 start, T2... var)
{
cout<<"sizeof ... now is: "<<sizeof... (var)<<endl;
cout<<start<<endl;
print(var...);
}


int main(void)
{
print(1,2,3,4);
return 0;
}

其中的聲明其實(shí)是沒(méi)什么用的,只是告訴使用者可以按照這樣的格式使用,如果不做這個(gè)聲明,只保留"邊界條件"和"遞歸的特例化定義",這樣雖然可行,但是未免會(huì)造成困惑

執(zhí)行結(jié)果如下:

如何使用c中變長(zhǎng)參數(shù)

實(shí)際上,這個(gè)"變長(zhǎng)"付出的代價(jià)還是很大的,要遞歸的實(shí)例出n個(gè)函數(shù),最終再調(diào)用邊界條件的函數(shù)。過(guò)程如下

如何使用c中變長(zhǎng)參數(shù)

如何使用c中變長(zhǎng)參數(shù)

2.使用va_list()函數(shù)實(shí)現(xiàn)變長(zhǎng)參數(shù)列表

以一個(gè)矩陣自動(dòng)識(shí)別維度的程序?yàn)槔?/p>

arrayMat.h

#include<iostream>
#include<string>
#include<stdarg.h>
using namespace std;
typedef int dtype;

class mat
{
public:
	mat();
	~mat();
	void set_dim(int dim);
	void mat::set_mat_shape(int i,...);
	int  get_dim();
	int* get_mat_shape();
	void print_shape();
	dtype* make_mat();
private:
	int dim;
	int *shape;
	dtype *enterMat;
};

arrayMat.cpp

#include"arrayMat.h"
mat::mat()
{
}

mat::~mat()
{
}

int mat::get_dim() {
	return this->dim;
}

int * mat::get_mat_shape() {
	return this->shape;
}

void mat::print_shape()
{
	for (int a = 0; a < this->dim; a++) {
		std::cout << shape[a] << " " ;
	}
}



void mat::set_dim(int i) {
	this->dim = i;
}

void mat::set_mat_shape(int i, ...) {
	va_list _var_list;
	va_start(_var_list, i);
	int count = 0;
	int *temp=new int[100];
	while (i != -1) {
		//cout << i <<" ";
		temp[count] = i;
		count++;
		i = va_arg(_var_list, int);
	}
	va_end(_var_list);
	this->set_dim(count);
	this->shape = temp;
	//std::cout << std::endl;
	//this->shape = new int [count];
	//for (int j = 0; j < count; j++)
		//shape[j] = temp[j];
}

//Mat2D A[i][j] = B[i + j * rows]

main.cpp

#include"arrayMat.h"


int main() {
	mat m1,m2;
	m1.set_mat_shape(1,3,128,128,-1);
	int *shape = m1.get_mat_shape();
	int dim = m1.get_dim();
	cout << "dim: " << dim<<endl;
	for (int i = 0; i < dim; i++)
		cout <<*(shape+i) <<" ";
	m1.print_shape();
	//m1.make_mat();
	//m2.set_mat_shape(3,3);
	//m2.make_mat();
	//m2.print_mat();
	return 0;
}

運(yùn)行結(jié)果:

如何使用c中變長(zhǎng)參數(shù)

到此,相信大家對(duì)“如何使用c中變長(zhǎng)參數(shù)”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI