获取qlist大小作为结构大小C ++ [英] Get a qlist size as a struct size C++

查看:213
本文介绍了获取qlist大小作为结构大小C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我怎样才能获得像sizeof struct这样的自己列表的大小。我知道sizeof是如何工作的。

Hello, How can i get the size of my own list like sizeof struct. I know how sizeof works for example.

enum NETCDF_Type
	{
		NETCDF_NONE,      //!< unknown data foramt
		NETCDF_STRING,  
		NETCDF_UCHAR,		//!< signed 1 byte integer
		NETCDF_CHAR,		//!< signed 1 byte integer
		NETCDF_SHORT, 	//!< signed 2 byte integer
		NETCDF_INT,		//!< signed 4 byte integer
		NETCDF_USHORT,	//!< unsigned 2-byte int
		NETCDF_UINT,		//!< unsigned 4-byte int
		NETCDF_FLOAT32,	//!< signed 4-byte int
		NETCDF_DOUBLE64,	//!< signed 8-byte int
		NETCDF_LONGLONG,
		NETCDF_ULONGLONG
	};

void getSize( const QList< NETCDF_Type > myList )
{
     for( int i = 0; i < myList.count(); i++ )
     {
          //do something
          //I need a size of which NETCDF_Type i am using.
          //Without sizeof();
     }
}





我尝试过:





What I have tried:

struct MyStruct
{
     char v1;
     char v2;
     char v3;
     short v4;
};

int mSize = sizeof( MyStruct ); mSize => 6;

struct MyStruct
{
     char v1;
     char v2;
     char v3;
     char v4;
     short v;
};


int mSize = sizeof( MyStruct ); mSize => 6;

推荐答案

这取决于您的要求,但简短的回答你不能: QList< T> 不是 POD ,而且你无法访问它实施细节。您可以通过列表大小将每个项目的大小乘以内存使用量的估算,即 sizeof(T)* myqlist.size()





[更新]

It depends on your requirements, but the short answer is you cannot: the QList<T> is not a POD and, moreover, you do not have access to its implementation details. You can get an extimate of the memory usage multipling the size of each item by the size of the list, i.e. sizeof(T) * myqlist.size().


[update]
Quote:

//我需要一个我正在使用的NETCDF_Type的大小。

//没有sizeof();

//I need a size of which NETCDF_Type i am using.
//Without sizeof();

我会为此编写一个方法,例如

I would write a method for that, e.g.

size_t getSize(NETCDF_Type typ);



方法实现由您直接决定,使用开关

[/ update]


The method implementation is up to you, in a straightforward approach, use a switch.
[/update]


这篇关于获取qlist大小作为结构大小C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆